我有一个我无法弄清楚的mod_rewrite重定向问题。
来自特定域的所有请求都“无声地”重写到指定的子目录中。例如www.mydomain.net/hello.html
检索/net/hello.html
中的文件。以下.htaccess (放置在我的托管根目录中)完美实现了这一目标:
RewriteEngine on
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200 # <-- i will need this later. read to the end of the post.
RewriteRule .* - [L]
rewriteCond %{HTTP_HOST} ^www.mydomain.net$
rewriteCond %{REQUEST_URI} !^/net.*$
rewriteRule (.*) /net/$1 [L]
然而,直接URL到这个目录但是应该用301重新定向到URL 而不用那个子目录。例如www.mydomain.net/net/hello.html
应重定向到www.mydomain.net/hello.html
(仍然会检索/net/hello.html
中的文件)。我的(放在/net
)的.htacces文件很遗憾不起作用:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^(.*) /$1 [R=301,L]
我得到了一个不定式的重定向循环,尽管根.htaccess文件中有RewriteCond %{ENV:REDIRECT_STATUS} 200
块...所以有什么问题?
答案 0 :(得分:1)
检查 THE_REQUEST 中的HTTP request line:
RewriteCond %{THE_REQUEST} ^GET\ /net[/? ]
RewriteRule ^net($|/(.*)) /$2 [L,R=301]