我正在清理我的域的目录结构(在根目录中设置根 url 的内容对我来说是不幸的!),并且需要一些关于如何正确使用 RewriteRule 的见解。
要点
我希望domain.tld使用domain.tld/subdirectory/,但在网址中仍显示为domain.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 效果很好。不幸的是,最后一个 RewriteRule 似乎应该足够简单,但事实并非如此。根目录中的旧内容设置仍然出现。
我在这里缺少什么?
更新:已解决
简单的修复,我对 .htaccess/apache 的经验不足,无法解释原因。
我有:
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 斯皮罗斯
关于apache - .htaccess:将根 url 重定向到子目录,但保留根 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6774421/