简体   繁体   English

用mod_rewrite更改文件名?

[英]change file name with mod_rewrite?

If you have a file /foo/index.html that you want to access via /bar/index.html that's easy enough with a rewrite: 如果你有一个文件/foo/index.html那你想通过访问/bar/index.html这是一个改写很容易:

RewriteRule ^bar/(.*)$    foo/$1 [L] # <-- Leaves URL the same, but gives content of foo/

But that means both /foo/index.html and /bar/index.html now work to access the content. 但这意味着/foo/index.html/bar/index.html现在都可以访问内容了。 Is there a way to now disallow access via /foo/index.html ? 有没有办法现在禁止通过/foo/index.html访问? Just doing: 只是做:

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/

causes a redirect loop. 导致重定向循环。 Is there a way to indicate "redirect foo to bar, unless the URL is already '/bar'"? 有没有办法表明“将foo重定向到bar,除非URL已经是'/ bar'”?

Yeah, but you need to do a check against something like %{THE_REQUEST} otherwise your two rules are going to keep changing each other's rewrites and loop. 是的,但是您需要对%{THE_REQUEST}类的内容进行检查,否则您的两个规则将不断更改彼此的重写和循环。 So something like: 所以像这样:

# 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 check against the %{THE_REQUEST} variable ensures that the actual request was made for /foo , and not an internally rewritten request. %{THE_REQUEST}变量的检查确保了实际的请求是针对/foo ,而不是内部重写的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM