2010-10-01 107 views
0

我正在檢查審查頁面的URL結構,可以按城市,縣,明星和技術人員進行篩選。我想讓頁面看起來像是靜態的HTML頁面,並用連字符代替加號。 我的目標: -Apply新樣式的URL,而無需修改評論網頁,只需將htaccess文件 - 設置 高達301從舊樣式的URL新樣式的URL重定向 - 轉換的長處爲連mod_rewrite可能無限循環?

目前審覈狀態的網址是這樣的:

domain.com/reviews/?city=City+Name

domain.com/reviews/?city=City+Name &頁= 2

domain.com/reviews/?county=County+Name

domain.com/reviews/?county=County+Name &頁= 2

domain.com/reviews/?stars=4

domain.com/reviews/?stars=4 &頁= 2

domain.com/reviews/?tech=Tech+Name

domain.com/reviews/?tech=Tech+Name &頁= 2

我的目標是讓他們看起來像這樣(和301舊式網址爲新樣式的網址):

domain.com/reviews/city/City-Name.html

domain.com/reviews/city/City-Name/page/2.html

domain.com/reviews/county/County-Name.html

domain.com/reviews/county/County-Name /page/2.html

domain.com/reviews/stars/4.html

domain.com/reviews/stars/4/page/2.html

domain.com/reviews/tech/Tech-Name.html

domain.com/reviews/tech/Tech-Name/page/2.html

市,縣和技術名稱不總是包含加號(即波士頓VS新+紐約)和一些有超過一個加號。目前,這是我所掩蓋了新樣式的URL舊式的網址(這順便說一句正常工作):

##FIRST PAGE REVIEWS (ie. reviews/city/city-name.html) 
RewriteRule ^reviews/(.*)/(.*)\-(.*)\-(.*)\-(.*)\-(.*)\.html "/reviews/?$1=$2+$3+$4+$5+$6" [NC,L] 
RewriteRule ^reviews/(.*)/(.*)\-(.*)\-(.*)\-(.*)\.html "/reviews/?$1=$2+$3+$4+$5" [NC,L] 
RewriteRule ^reviews/(.*)/(.*)\-(.*)\-(.*)\.html "/reviews/?$1=$2+$3+$4" [NC,L] 
RewriteRule ^reviews/(.*)/(.*)\-(.*)\.html "/reviews/?$1=$2+$3" [NC,L] 
RewriteRule ^reviews/(.*)/(.*)\.html "/reviews/?$1=$2" [NC,L] 

##PAGINATED REVIEWS (ie. reviews/city/city-name/page/2.html) 
RewriteRule ^reviews/(.*)/(.*)\-(.*)\-(.*)\-(.*)\-(.*)/page/(.*)\.html "/reviews/?$1=$2+$3+$4+$5+$6&page=$7" [NC,L] 
RewriteRule ^reviews/(.*)/(.*)\-(.*)\-(.*)\-(.*)/page/(.*)\.html "/reviews/?$1=$2+$3+$4+$5&page=$6" [NC,L] 
RewriteRule ^reviews/(.*)/(.*)\-(.*)\-(.*)/page/(.*)\.html "/reviews/?$1=$2+$3+$4&page=$5" [NC,L] 
RewriteRule ^reviews/(.*)/(.*)\-(.*)/page/(.*)\.html "/reviews/?$1=$2+$3&page=$4" [NC,L] 
RewriteRule ^reviews/(.*)/(.*)/page/(.*)\.html "/reviews/?$1=$2&page=$3" [NC,L] 

說我有是301-ING舊式的URL到新的麻煩的部分樣式的網址。這是我到目前爲止:

RewriteCond %{QUERY_STRING} (.*)=(.*) [NC] 
RewriteRule ^reviews/(.*) /reviews/%1/%2.html? [NS,R=301,NC] 

RewriteCond %{QUERY_STRING} (.*)=(.*)&page=(.*) [NC] 
RewriteRule ^reviews/(.*) /reviews/%1/%2/page/%3.html? [NS,R=301,NC] 

當我嘗試這個FireFox給我的「該頁面沒有正確重定向」錯誤。有任何想法嗎?

回答

0

是的,這可能是一個無限循環。使用THE_REQUEST而不是檢查request line

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?([^=]+)=([^\ ]*)&page=([^\ ]*) [NC] 
RewriteRule ^reviews/ /reviews/%1/%2/page/%3.html? [NS,R=301,NC] 
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?([^=]+)=([^\ ]*) 
RewriteRule ^reviews/ /reviews/%1/%2.html? [NS,R=301,NC] 

注意,如果參數的數量和順序是與此不同,這將失敗。

+0

非常好!謝謝你的工作。唯一的是我需要將這些加號轉換爲連字符。目前「/ reviews /?city = City + Name」將帶您到「/reviews/city/City+Name.html」,它應該是「... City-Name.html」。還有一種方法可以將所有內容都轉換爲小寫? – Yev 2010-10-01 18:10:11

+0

@YevgeniyЖеняVan Chuchin:雖然mod_rewrite可以實現,但它相當複雜,因爲您一次只能替換固定數量的事件(請參閱http://stackoverflow.com/questions/3791548/mod-rewrite -rule到強制規範%的編碼/ 3806538#3806538)。更高層次的語言更適合做這種工作。 – Gumbo 2010-10-01 18:16:15

+0

我沒有固定的金額。發生了5次就足夠了,這就是我在第一部分中所做的事情(用新風格掩蓋舊式URL)。 – Yev 2010-10-01 18:19:35