2014-09-02 62 views
1

我有一個htaccess重定向,像這樣:爲什麼使用%{query_string}的htaccess rewritecond僅在URL包含index.php時才起作用?

RewriteCond %{QUERY_STRING} tabid=53 
RewriteRule . http://www.example.com/foobar? [R=301,L] 

所以,當我訪問example.com/?tabid=53沒有任何反應,example.com/index.php?tabid=53被重定向到/foobar。當我添加另一個條件:

RewriteCond %{REQUEST_URI} ^(.*)$ 

什麼都不會改變,從我的理解,這應該說「如果在URI的index.php與否並不重要」。我究竟做錯了什麼?

+2

這是你已經設置了唯一的重寫規則?其他規則可能與它衝突。無論如何,您應該嘗試記錄mod_rewrite正在做什麼 - 請參閱rewriteloglevel指令文檔(http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloglevel for Apache 2.2,在2.4版中已更改) – 2014-09-02 11:06:16

回答

2

你有沒有嘗試過這樣的:

RewriteCond %{QUERY_STRING} tabid=53 
RewriteRule ^(.*)$ http://www.example.com/foobar? [R=301,L] 

我假設你的一個點是問題,這點匹配任何單個字符,但有沒有在你的example.com/?tabid=53匹配。如果您使用example.com/i?tabid=53,那麼它工作。

+0

提供一些描述 – 2014-09-02 11:17:12

+0

+1是的,這應該工作 – anubhava 2014-09-02 11:53:32

+0

實際上,這是因爲'重寫規則。 ','RewriteRule ^'工作:) – Alex 2014-09-03 07:20:01

2

您可以通過匹配.*而不是.來避免這種奇怪的行爲。
您也可以匹配root levelindex.php

兩種解決方案都按預期工作。

解決方案1 ​​

RewriteEngine On 

RewriteCond %{QUERY_STRING} tabid=53 [NC] 
RewriteRule .* /foobar? [R=301,L] 

解決方案2

RewriteEngine On 

RewriteCond %{QUERY_STRING} tabid=53 [NC] 
RewriteRule ^(|index\.php)$ /foobar? [R=301,L] 

結論

與規則的問題

RewriteRule . http://www.example.com/foobar? [R=301,L] 

是因爲.意味着one character並且從不匹配,因爲它是空的(沒有文件,直接在根級查詢字符串)。

這就是爲什麼.*比賽(指0 or more characters)或RewriteRule ^(|index\.php)$(這意味着match root level -empty- or index.php

+0

+1這也應該工作。 – anubhava 2014-09-02 11:54:12

相關問題