我正在清理域的目录结构(在根目录中设置根 url 的内容对我来说是不幸的!)并且需要一些关于如何正确使用 RewriteRule 的见解。
The Gist
I want 域名.tld to use 域名.tld/子目录/但仍然显示为域名.tld在网址中。
到目前为止我的 .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 效果很好。不幸的是,似乎是最后一个 RewriteRuleshould工作很简单,但事实并非如此。根目录中的旧内容设置仍然出现。
我在这里缺少什么?
更新:已解决
简单的修复,我对 .htaccess / apache 的经验不足,无法解释原因。
I had:
RewriteRule ^/$ /subdirectory/?$ [L]
删除一个斜杠修复了所有问题:
RewriteRule ^$ /subdirectory/?$ [L]
所以现在我的问题是:这是为什么?
尝试这个:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subdirectory/
# REWRITES ALL URLS
# [REPLACE "domain" WITH THE ACTUAL DOMAIN,
# WITHOUT THE TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
# REWRITE ALL THOSE TO subdirectory
RewriteRule ^(.*)$ /subdirectory/$1 [L]
BR
斯皮罗斯
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)