2009-08-07 199 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]