2012-10-21 65 views
1

所以我有兩條規則。每條規則都完全按照它自己在.htaccess中的內容執行,但是當兩條規則都在那裏時,它們就開始相互衝突。兩個RewriteCond和RewriteRule獨立工作

RewriteCond %{REQUEST_URI} ^(.*)\.html$ 
RewriteRule ^(.*)\.html$ index.php?id=$1 [QSA,L] 

上述規則越來越說http://domain.com/12.html和傳球次數12index.php只有工作正常,如果數量與.html結束(注意:12.html文件不存在!)

RewriteCond %{REQUEST_URI} !^(.*)\.html$ 
RewriteRule ^(.*)$ /$1\.html [L,R=301] 

該作品可以檢查URL是否以.html結尾。如果它沒有完成.html,它會將其重定向到URI + .html(這也可以很好地工作)。

當我在一個.htaccess

RewriteCond %{REQUEST_URI} ^(.*)\.html$ 
RewriteRule ^(.*)\.html$ index.php?id=$1 [QSA,L] 

RewriteCond %{REQUEST_URI} !^(.*)\.html$ 
RewriteRule ^(.*)$ /$1\.html [L,R=301] 

兩個規則則有導致重定向循環迴路。

有人可以指出我哪裏出錯了嗎?

+0

如果您只是使用第二個重寫規則,它會對'http://domain.com/index.php?id = 12'做什麼? – Neil

+0

它returnn:http://domain.com/index.php.html?id=12 –

回答

1

因此,第一條規則將http://domain.com/12.html重寫爲http://domain.com/index.php?id=12。然後事情開始出錯;第二條規則將其重寫爲http://domain.com/index.php.html?id=12,但在那一點上,我認爲你會發現第一條規則再次啓動,將其重寫爲http://domain.com/index.php?id=index.php,第二條規則重寫爲http://domain.com/index.php.html?id=index.php。在這一點上,我認爲規則最終會在URL上發生爭執。

最簡單的解決方案可能是調整第二個重寫規則,而不是重寫已經包含擴展名的頁面,即使用[^.]*而不是.*

+0

你的解決方案確實工作的任何id這是一個整數,但遺憾的是它不工作,當他的ID是像123.23我說小數繼續試驗,並提出這個目前爲止似乎工作: - 'RewriteCond%{REQUEST_URI}!(。*)\。html $' - '''RewriteRule ^。* $ /$0\.html [L, R = 301]' - 'RewriteCond%{REQUEST_URI} ^(。*)\。html $' - 'RewriteRule ^(。*)\。html $ index.php?ip = $ 1 [QSA,L]' –

+0

對不起,你沒有提到你想要支持十進制數。在這種情況下,也許你可以用'[.0-9] *'代替? – Neil