【发布时间】:2011-07-21 10:08:18
【问题描述】:
我正在清理我的域的目录结构(对我来说,在根目录中设置根 url 的内容真是太糟糕了!)并且需要一些关于如何正确使用 RewriteRule 的见解。
要点
我希望 domain.tld 使用 domain.tld/subdirectory/ 但仍然在 url 中显示为 domain.tld。 p>
我的 .htaccess 到目前为止
Options +FollowSymlinks
RewriteEngine On
#Force removal of wwww subdomain
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.tld
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
#Trailing slash
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
#Redirect root url to subdirectory
RedirectMatch 301 ^/subdirectory/$ http://domain.tld/
RewriteRule ^/$ /subdirectory/?$ [L]
RedirectMatch 效果很好。不幸的是,那里的最后一个 RewriteRule 似乎应该工作得足够简单,但事实并非如此。根目录中的旧内容设置仍然出现。
我在这里错过了什么?
更新:已解决
简单的修复,我对 .htaccess / apache 经验不足,无法解释原因。
我有:
RewriteRule ^/$ /subdirectory/?$ [L]
删除一个斜线可以解决所有问题:
RewriteRule ^$ /subdirectory/?$ [L]
所以现在我的问题是:为什么会这样?
【问题讨论】:
标签: apache .htaccess mod-rewrite