簡體   English   中英

用mod_rewrite更改文件名?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM