试图得到

www.example.com

直接转到

www.example.com/store

我已经尝试了多个代码位,但没有工作。

我的尝试:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]

我做错了什么?


当前回答

试试这个:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ store [L]

如果你想要一个外部重定向(这导致访问浏览器显示重定向URL),设置R标志也在那里:

RewriteRule ^$ /store [L,R=301]

其他回答

我搜索了一下,得到了这些结果:

RewriteEngine在RewriteBase上 / RewriteRule ^index.(.*)?$ http://domain.com/subfolder/ (r = 301) 这将使任何尝试转向 访问一个名为index的文件。的东西 您的子文件夹,是否文件 存在与否。

或者试试这个:

RewriteCond % {HTTP_HOST} 样品! ^ www。com美元(NC) RewriteRule ^(*)美元。 % {HTTP_HOST} - samlse / 1 (R = 301美元,L]

我没有在.htaccess文件中做很多重定向,所以我不确定这是否会工作。

要设置从根目录到子目录的不可见重定向,可以在/root/中使用下面的RewriteRule。htaccess:

RewriteEngine on

RewriteCond %{REQUEST_URI} !^/subfolder
RewriteRule ^(.*)$ /subfolder/$1 [NC,L]

上面的规则将从内部重定向浏览器:

http://example.com/

to

http://example.com/subfolder

And

http://example.com/foo

to

http://example.com/subfolder/foo

而浏览器将停留在根文件夹上。

我们也可以使用Redirect来实现这个目的

Redirect 301 / www.example.com/store

或用于映射的别名

Alias / /store

编辑:mod_alias只适用于httpd.conf。

参考资料

http://httpd.apache.org/docs/2.2/mod/mod_alias.html https://httpd.apache.org/docs/trunk/rewrite/avoid.html

如果文件在根目录中不存在,这将尝试子目录。需要这个,因为我移动了一个基本的。html网站,期望在根级运行,并将其推到一个子目录。只有在所有文件都是平面的情况下才有效(在子目录中不可能使用.htaccess技巧)。对于css和js文件这样的链接文件很有用。

# Internal Redirect to subdir if file is found there.
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/subdir/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /subdir/$1 [L]

尝试在htaccess中使用下面的行

注意:你可能需要检查default.html的名称是什么

html是根文件夹中默认加载的文件。

RewriteEngine

重定向/default.html http://example.com/store/