Apache-2.2

對整個站點 RedirectMatch 規則進行例外處理

  • November 25, 2020

我們有這個 RedirectMatch 規則可以在啟用 SSL 的站點上移動客戶端,同時保持他們嘗試訪問的路徑:

Redirectmatch permanent (.*) https://www.foobar.com$1

它執行良好。現在我們想為 /somepath 添加一個不應重定向的異常。我怎樣才能做到這一點?

使用 mod_rewrite 我會添加一個重寫規則,說“不要重寫並停止匹配”,但是重定向沒有這種機制。此外,如果我像 !(/somepath|/someotherpath) 這樣簡單地反轉正則表達式,它將不再獲得 $1 參數。

有什麼理由不想使用 mod_rewrite?

你可以模仿這個功能:

RewriteCond %{REQUEST_URI} !^/(somepath|someotherpath)
RewriteRule (.*) $1 [R=永久]
重寫規則 http://%{SERVER_NAME}(.*) https://www.foobar.com$1 [L]

我沒有意識到 Apache 使用 PCRE。既然這樣做了,你就可以用 mod_alias 做一些巫術來做你想做的事:

RedirectMatch 永久 ^/? ((?! (Thisisfoo | thisisbar)).*) https://www.foobar.com/$1

其中 /thisisfoo 和 /thisisbar 是重定向的例外。

引用自:https://serverfault.com/questions/189666