2013-08-05 42 views
2

我想將路徑/health重寫爲/health.php在將[RewriteRule]與[L]標誌匹配後,重寫不會停止

我現在有有以下.htaccess文件,具有包羅萬象的規則:

RewriteEngine On 
RewriteRule ^health$ health.php [L] 
RewriteRule ^.*$ index.php [L] 

但是:

RewriteEngine On 
RewriteRule ^.*$ index.php [L] 

所以,我只是之前雜物箱增加了一個新的規則令我驚訝的是,當我在瀏覽器中請求/health時,它不會重寫爲health.php

但是,當我刪除最後一條(全部捕捉)規則時,它按預期工作。

爲什麼mod_rewrite在將規則與[L]標誌匹配後停止處理規則?

回答

4

找到它。我一直認爲[L]標誌停止了處理所有規則的時期。實際上,它只是停止當前規則集中的處理規則,但是會在重寫的請求上重新運行整個規則集。

因此,我包羅萬象的代碼終於趕上它,因爲rewrite log顯示:

strip per-dir prefix: [...]/public/health -> health 
applying pattern '^health$' to uri 'health' 
rewrite 'health' -> 'health.php' 
add per-dir prefix: health.php -> [...]/public/health.php 
strip document_root prefix: [...]/public/health.php -> /health.php 
internal redirect with /health.php [INTERNAL REDIRECT] 

strip per-dir prefix: [...]/public/health.php -> health.php 
applying pattern '^health$' to uri 'health.php' 
strip per-dir prefix: [...]/public/health.php -> health.php 
applying pattern '^.*$' to uri 'health.php' 
rewrite 'health.php' -> 'index.php' 
add per-dir prefix: index.php -> [...]/public/index.php 
strip document_root prefix: [...]/public/index.php -> /index.php 
internal redirect with /index.php [INTERNAL REDIRECT] 

strip per-dir prefix: [...]/public/index.php -> index.php 
applying pattern '^health$' to uri 'index.php' 
strip per-dir prefix: [...]/public/index.php -> index.php 
applying pattern '^.*$' to uri 'index.php' 
rewrite 'index.php' -> 'index.php' 
add per-dir prefix: index.php -> [...]/public/index.php 
initial URL equal rewritten URL: [...]/public/index.php [IGNORING REWRITE] 

的解決方案,在this blog發現,包括把有條件的包羅萬象的規則之前,只執行它當以前沒有處理過內部重定向時:

RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteRule ^.*$ index.php [L]