Wil*_*ips 6 apache .htaccess mod-rewrite
我正在清理域名的目录结构(对我来说,在根目录中设置根URL的内容很糟糕!)并且需要一些关于如何正确使用RewriteRule的见解.
要旨
我希望domain.tld使用domain.tld /子目录/但仍然在url中显示为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]
Run Code Online (Sandbox Code Playgroud)
RedirectMatch效果很好.不幸的是,似乎最后的RewriteRule 应该足够简单,但事实并非如此.仍会显示根目录中的旧内容设置.
我在这里错过了什么?
更新:已解决
简单的修复,我没有足够的经验与.htaccess/apache能够解释原因.
我有:
RewriteRule ^/$ /subdirectory/?$ [L]
Run Code Online (Sandbox Code Playgroud)
删除一个斜杠修复了所有内容:
RewriteRule ^$ /subdirectory/?$ [L]
Run Code Online (Sandbox Code Playgroud)
所以现在我的问题是:为什么?
a 中的 URI 部分RewriteRule
不包含前面的斜杠。
因此,为了匹配,http://example.com/
您必须使用理论上匹配的RewriteRule ^$
位置(注意双斜杠)。RewriteRule ^/$
http://example.com//
另外:?$
最后没有任何意义,因为这会添加一个带有空参数的查询字符串$
。
最后你有:RewriteRule ^$ /subdirectory/ [L,QSA]