1

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]
Qiao
  • 16,565
  • 29
  • 90
  • 117
  • Duplicate: http://stackoverflow.com/questions/1513308/question-mark-in-the-end-of-rewriterule – Gumbo Oct 03 '09 at 09:46

2 Answers2

0

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

  • if RewriteRule ^oldpage\.php$ http://new-site.com/newpage-%1? , than it is ok http://stackoverflow.com/questions/1513308/question-mark-in-the-end-of-rewriterule – Qiao Oct 03 '09 at 09:23
0

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
  • 643,351
  • 109
  • 780
  • 844
  • 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 Oct 03 '09 at 09:05
  • Its ok it is Firefox remembering this. Strange. – Qiao Oct 03 '09 at 09:13