[英]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.