2009-08-07 198 views
2

我有這個網址:重寫查詢字符串

oldsite.com/profile.php?uid=10

我想將它改寫爲:

newsite.com/utenti/10

我怎樣才能做到這一點?

更新:我寫了這個:

RewriteCond %{QUERY_STRING} ^uid=([0-9]+)$ 
RewriteRule ^profile\.php$ http://www.newsite.com/utenti/$1 [R=301,L]

但是$ 1場完整的查詢字符串,而不僅僅是用戶ID。

回答

5

要在重寫條件中使用匹配,您必須使用%1而不是$ 1。另外,如果你想刪除查詢字符串的其餘部分,你必須添加一個?

 
RewriteCond %{QUERY_STRING} ^uid=([0-9]+)$ 
RewriteRule ^profile\.php$ http://www.newsite.com/utenti/%1? [R=301,L] 
2

$n僅指RewriteRule指令的比賽。使用%n來引用對應的RewriteCond指令的匹配。

此外,您需要爲替代指定一個空查詢。否則,將使用原始查詢。

如果你想有查詢的其餘部分保持不變,使用這樣的規則:

RewriteCond %{QUERY_STRING} ^(([^&]*&)*)uid=([0-9]+)(.*) 
RewriteRule ^profile\.php$ http://new.example.com/utenti/%3?%1%4 [R=301,L]