重写查询string

我有这个url:

  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匹配完整的查询string,而不仅仅是用户ID。

“$ 1”匹配同一行的第一对括号(RewriteRule); 你需要“%1”,它匹配/上一行/第一对括号 – RewriteCond:

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

更好的方法是只用1行(一个RewriteRule)来完成,但如果uid在QueryString中,则不能这样做。

 <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{SERVER_NAME} ^oldsite.com$ RewriteCond %{QUERY_STRING} uid=([0-9]+) RewriteRule ^/profile\.php http://newsite.com/utenti/%1 [NC,R=301,L] </IfModule> 

重点是使用%1,而不是$ 1