试图得到

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

试试这个:

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]

上述大多数解决方案都是正确的,但它们都缺少重定向的透明性。

在我的例子中,当访问www.example.com时,我想重定向到子目录/store,但不更新URL到www.example.com/store。(所有我想要的是获得该目录的页面代码)。如果这是你的情况,下面的解决方案是完美的。

RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /store/$1 [L]

来源:http://wiki.dreamhost.com/Transparently_redirect_your_root_directory_to_a_subdirectory

如果你想重写URL并隐藏原始URL,另一个选择:

RewriteEngine on
RewriteRule ^(.*)$ /store/$1 [L]

例如,如果您键入http://www.example.com/product.php?id=4,它将透明地打开http://www.example.com/store/product.php?id=4文件,但不会向用户显示完整的url。

您只需将此代码添加到.htaccess文件中

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /folder/index.php [L]
</IfModule>