试图得到

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]

我做错了什么?


当前回答

如果你想重写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。

其他回答

试试这个:

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]

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

我认为你发布的代码的主要问题是:

第一行在严格以sample.com开头的主机上匹配,因此www.sample.com不匹配。 第二行至少需要一个字符,然后是www.sample.com,它也不匹配(为什么要转义第一个w?) 所包含的规则都没有重定向到您在目标中指定的url(另外,sample被拼错为samle,但这无关紧要)。

作为参考,以下是您当前拥有的代码:

Options +FollowSymlinks
RewriteEngine on

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

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

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

在我的例子中,当访问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

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