Forum Moderators: phranque

Message Too Old, No Replies

How to catch a question mark pf URL parameters while rewriting?

If question mark is already included in the rewrite pattern?

         

craig1972

12:55 am on Apr 28, 2009 (gmt 0)

10+ Year Member



Hi.

For my site I have a simple rewrite instruction:


RewriteRule ^/onward/(.*)$ /scripts/sendto?url_id=$1 [L]

So any link like


http://example.com/onward/somewhere

will convert into


http://example.com/scripts/sendto?url_id=somewhere

Now I also want this to work if the user enters a URL like this:


http://example.com/onward/somewhere?a=b

But this converts into:


http://example.com/scripts/sendto?url_id=somewhere?a=b

That's two question marks! So it doesn't work. Any thoughts on how to trap the second question mark?

jdMorgan

1:50 am on Apr 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



But this converts into:

http://example.com/scripts/sendto?url_id=somewhere?a=b

That's two question marks!

It's also an invalid URL -- and an unexpected result. By default, mod_rewrite replaces the existing query string with any query string specified in the RewriteRule substitution, unless the [QSA] flag is used or the pre-existing query string is captured by a RewriteCond and then back-referenced in the substitution.

So the expected result is that the "a=b" name/value pair should have been dropped/ignored/lost by your rule, and the result should have been the same as if it were not present in the original request.

If your server is really generating the /scripts/sendto?url_id=somewhere?a=b as the result of your rule, you need to ask your host to re-install mod_rewrite and/or Apache, because it is seriously broken.

If you haven't actually confirmed that the double-question mark invalid string is what is actually output by your rule, then it's more likely that all you need to do is to add the [QSA] flag (Query String Append) to your rule, making the flags "[QSA,L]". This tells mod_rewrite to append the new query string to the existing one, rather than replacing it. See the Apache mod_rewrite documentation for more info.

Jim

craig1972

9:58 am on Apr 28, 2009 (gmt 0)

10+ Year Member



QSA flag was it. Thanks!