views:

756

answers:

2

I have this URL:

oldsite.com/profile.php?uid=10

I would like to rewrite it to:

newsite.com/utenti/10

How can I do that?

UPDATE: I wrote this:

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

But $1 match the full query string and not just the user id.

+1  A: 

To use matches in the rewrite conditions, you have to use %1 instead of $1. Also, if you wish to remove the rest of the query string you have to append a ?

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

The $n only refer to the matches of the RewriteRule directive. Use %n to reference the matches of the corresponding RewriteCond directive.

Additionally you need to specify an empty query for the substitution. Otherwise the original query will be used.

And if you want to have the rest of the query to stay intact, use this rule:

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