[英]change file name with mod_rewrite?
如果你有一个文件/foo/index.html
那你想通过访问/bar/index.html
这是一个改写很容易:
RewriteRule ^bar/(.*)$ foo/$1 [L] # <-- Leaves URL the same, but gives content of foo/
但这意味着/foo/index.html
和/bar/index.html
现在都可以访问内容了。 有没有办法现在禁止通过/foo/index.html
访问? 只是做:
RewriteRule ^foo/(.*)$ bar/$1 [R=301,L] # <-- Change the URL if accessed via /foo
RewriteRule ^bar/(.*)$ foo/$1 [L] # <-- Leaves URL the same, but gives content of foo/
导致重定向循环。 有没有办法表明“将foo重定向到bar,除非URL已经是'/ bar'”?
是的,但是您需要对%{THE_REQUEST}
类的内容进行检查,否则您的两个规则将不断更改彼此的重写和循环。 所以像这样:
# this is the rule you already had:
RewriteRule ^bar/(.*)$ foo/$1 [L]
# this is the rule that externally redirects
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo/
RewriteRule ^foo/(.*)$ /bar/$1 [L,R=301]
对%{THE_REQUEST}
变量的检查确保了实际的请求是针对/foo
,而不是内部重写的请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.