views:

195

answers:

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

In first case result is
new-site.com/newpage-3?id=3
in second
new-site.com/newpage-3

What does question mark in second rewrite rule means?

+2  A: 

The ? at the end of a destination (destinations are not regular expressions) means to go to that destination with no query string.

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

If the query string contains only an id, it stores the value which is then used in the destination, so if you have

http://foo.com/oldpage.php?id=54

you'll end up with

http://new-site.com/newpage-54?id=54

If you have

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

You'll go to the same destination but with an empty query string, so going to

http://foo.com/oldpage.php?id=54

will end up in

http://new-site.com/newpage-54
Vinko Vrsalovic
"If the query string contains only an id" and if not, is it only true for id?
Qiao
The RewriteCond %{QUERY_STRING} ^id=(.*)$ means that the rule only applies if the query string matches. In this case it'll match any query string that starts with id=
Vinko Vrsalovic