views:

340

answers:

2

I am moving my site to new domain. Need to redirect pages
from
old-site.com/oldpage.php?id=X
to
new-site.com/newpage-X
(X is number)

Why this rule does not work?

RewriteEngine on
RewriteRule ^oldpage.php?id=(.*)$ http://new-site.com/newpage-$1 [R=301,L]
A: 

I suspect that you will need to use QUERY_STRING

RewriteCond %{QUERY_STRING}  ^id=(.*)$
RewriteRule ^oldpage\.php$ http://new-site.com/newpage-%1 [R=301,L]

Hope this helps

Cameron Spiers
it adds ?id=X to new url - http://new-site.com/newpage-3?id=3
Qiao
if RewriteRule ^oldpage\.php$ http://new-site.com/newpage-%1? , than it is okhttp://stackoverflow.com/questions/1513308/question-mark-in-the-end-of-rewriterule
Qiao
A: 

The RewriteRule does only operate on the URL path and not the URL query. You need to use the RewriteCond directive to test the URL query (%{QUERY_STRING}). So try this rule:

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

This rule will also preserve other parameters in the query.

Gumbo
thanks, it works. but several urls that i tried early with wrong rules still redirect to wrong page. Is it server problem? Why it remember this?
Qiao
Its ok it is Firefox remembering this. Strange.
Qiao