Apache 忽略 RewriteCond 和 RewriteRule

Apache 忽略 RewriteCond 和 RewriteRule

我在這裡要瘋了。 Apache 完全忽略了RewriteCondRewriteRule。我希望這會出錯,因為 RewriteEngine 尚未設定為On。我有一堆其他的RewriteCondRewriteRule行工作正常,當我檢查重寫日誌(我已啟用)時,它顯示這兩個被跳過。我修剪了其他所有內容,以驗證沒有任何一個因素導致了問題,果然,Apache 很高興地在這個配置中沒有出現任何錯誤:

DocumentRoot /var/sites/public
<Directory /var/sites/public>
    AllowOverride None
    SetEnv APPLICATION_ENV production

    RewriteCond %{HTTP_HOST} !^$
    RewriteRule ^/(.*) http://www.example.com/$1 [L,R]

    Order allow,deny
    allow from all
</Directory>

我很確定這裡有一些明顯的錯誤,我因為盯著它太久而被蒙蔽了。任何幫助,將不勝感激。

注意:我的虛擬主機中沒有這個。配置中沒有任何虛擬主機。我已經刪除了 RewriteCond,它仍然不會拋出任何錯誤。我也用 RewriteEngine On 嘗試過這個。

答案1

快速瀏覽一下它就知道 RewriteCond 不會與任何內容匹配,除非看起來什麼都沒有。不知道 mod_rewrite 對此有何感想。

您究竟想用該規則做什麼?

例如,如果您希望它匹配 (www.)example.com 之外的任何網域並將其轉發到 www.example.com,那麼您將擁有以下規則:

RewriteCond %{HTTP_HOST} !^(www.)?example\.com$
RewriteRule (.*) http://www.example.com/$1 [R,L]

答案2

你也想要這個:

RewriteCond %{HTTP_HOST} .

……或者用不同的方式寫:

RewriteCond %{HTTP_HOST}!=""

所以:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^(www.)?example\.com$
RewriteRule (.*) http://www.example.com/$1 [R,L]

相關內容