2011-08-29 65 views
13

我有一個問題,重寫一個URL。 我想這一點:RewriteCond帶參數的網址

http://www.foo.com/test.php?u=s1&id=12345&img=12 

http://app.foo.com/12345-s1-12.test 

第一個參數u是一個字符串,參數id和IMG是整數。

我已經開始用類似的東西:

RewriteCond %{REQUEST_URI} ^/test.php?u=(.*)&id=(.*)&img=(.*)/ [NC] 
RewriteRule (.*) http://app.foo.com/%2-%1-%3.test [QSA,R=301,L] 

謝謝:)

編輯:

還不行,但我接近!

RewriteCond %{REQUEST_URI} ^/test.php [NC] 
RewriteCond %{QUERY_STRING} ^u=(.*)&id=(.*)&img=(.*) 
RewriteRule (.*) http://app.foo.com/%2-%1-%3.test [QSA,R=301,L] 

現在,它給我的鏈接:

http://app.foo.com/12345-s1-12.test?u=s1&id=12345&img=12 

相反的:

http://app.foo.com/12345-s1-12.test 

:(

回答

26

解決

RewriteCond %{REQUEST_URI} ^/test.php [NC] 
RewriteCond %{QUERY_STRING} ^u=(.*)&id=(.*)&img=(.*) 
RewriteRule (.*) http://app.foo.com/%2-%1-%3.test? [R=301,L] 

在RewriteRule的末尾放置一個?可刪除以前的查詢字符串信息。

-1

這完美的作品!我用這個 - 但我想知道爲什麼目標網址在最後顯示「%3f」。

我使用:

RewriteCond %{REQUEST_URI} ^/ie/xx/yy.php [NC] 
    RewriteCond %{QUERY_STRING} ^rubric=(.*)&nr=(.*) 
    RewriteRule (.*) https://www.newdom.com/xx/yy.php?rubric=%1&nr=%2 [R=301,L] 

爲什麼在最後%3F?