The question mark at the end of the RewriteRule

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 the first case the result is new-site.com/newpage-3?id=3
in the second
new-site.com/newpage-3

What does the question mark mean in the second rewrite rule?

+2


source to share


1 answer


What? at the end of a destination (destinations are not regular expressions) means jump to that destination without a query string.

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

      

If the query string only contains 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 get

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 will go to the same destination but with an empty query string, so go to

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

      

will end in

http://new-site.com/newpage-54

      

+8


source







All Articles