2012-01-23 33 views
1

我有我的.htaccess文件安裝如下。這是一個簡單的概念,其中所有請求都轉發到cgi-bin/controller.rb,除了那些請求/ images,/ css,/ javascripts或/ cgi-bin中的任何請求外,我還希望根被重定向到/ index/show,但那是錯誤的部分。它只是無限地重定向到/ index/showindex/showindex/show ...Htaccess RedirectMatch正確使用

任何人都可以解釋爲什麼我的redirectmatch不起作用?

RedirectMatch permanent ^/$ /index/show 

ErrorDocument 404 /404 

RewriteEngine on 
RewriteCond %{REQUEST_URI} !^/images/ [NC] 
RewriteCond %{REQUEST_URI} !^/css/ [NC] 
RewriteCond %{REQUEST_URI} !^/javascripts/ [NC] 
RewriteCond %{REQUEST_URI} !^/cgi-bin/ [NC] 
RewriteRule (.*) /cgi-bin/controller.rb?%{QUERY_STRING}&page=$1 [L] 

回答

3

避免同時使用RedirectMatch和RewriteRule。這些是不同的模塊,並且在請求流中的不同時間應用。你可以只使用重寫規則的實現相同的:

RewriteEngine on 

RewriteCond %{REQUEST_URI} !^/index/show [NC] 
RewriteRule ^$ /index/show [L] 

RewriteCond %{REQUEST_URI} !^/index/show [NC] 
RewriteCond %{REQUEST_URI} !^/images/ [NC] 
RewriteCond %{REQUEST_URI} !^/css/ [NC] 
RewriteCond %{REQUEST_URI} !^/javascripts/ [NC] 
RewriteCond %{REQUEST_URI} !^/cgi-bin/ [NC] 
RewriteRule (.*) /cgi-bin/controller.rb?page=$1 [L,QSA] 

可以選擇使用RewriteRule ^$ /index/show [L,R=301]如果你真的想重定向,而不是讓地址欄非常乾淨。

還記得要清除瀏覽器緩存,因爲永久重定向非常積極地緩存。

+0

完美工作。你的回答幫助我更多地瞭解了htaccess文件的工作原理。謝謝。 – weexpectedTHIS

+0

爲什麼要避免同時使用RedirectMatch和RewriteRule?它們是不同的模塊,並且在請求流中的不同時間應用並不構成理由。 –

+0

@ user2407309避免難以解釋不規則行爲,這在不同的服務器上可能會有所不同,這似乎是一種很好的做法。 – Gerben