I have this URL:


I would like to rewrite it to:


How can I do that?

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.

2 Answers 2


"$1" matches the first pair of brackets on the same line (the RewriteRule); you need "%1", which matches the first pair of brackets on the /previous/ line - the RewriteCond:

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

A better way would be to do it with only 1 line (a RewriteRule), but you can't do that if the uid is in the 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]

the point is to use %1, not $1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.