试图得到

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]

我做错了什么?


当前回答

实现这一目标有两种可能的解决方案: 1. 在根文件夹中创建一个。htaccess文件,如下所示(只需将example.com和my_dir替换为相应的值):

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_dir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_dir/index.php [L] 
</IfModule>

使用RedirectMatch只重定向根URL“/”到另一个文件夹或URL, RedirectMatch ^/$ http://www.example.com/my_dir

其他回答

我发现为了避免循环重定向,重要的是将重定向的范围限制在根目录。 我会用:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/store [R=301,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]

如果文件在根目录中不存在,这将尝试子目录。需要这个,因为我移动了一个基本的。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]

我们也可以使用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

你可以使用一个重写规则,使用^$来表示根目录,并将其重写到你的/store目录,如下所示:

RewriteEngine On
RewriteRule ^$ /store [L]