Forum Moderators: phranque

Message Too Old, No Replies

How to strip the query string from a URL?

Should be easy with mod_rewrite, right?

         

MichaelBluejay

2:28 am on Apr 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I thought it would be simple to strip the query string with mod_rewrite, but all my attempts have failed. I'm just trying to take this:

http://mydomain.com/dir/?code

and change it to this:

http://mydomain.com/dir/

What I've discovered so far is that RewriteRule doesn't seem to be able to match a question mark, even if I'm careful to escape it with a slash -- /?. I tried:

RewriteCond %{QUERY_STRING}!^$
RewriteRule .* %{REQUEST_URI} [R,L]

I hoped that REQUEST_URI wouldn't include the query string, but apparently it does. And I get a "too many redirects" error when I use that code.

It'll probably be obvious when I see the answer....

jdMorgan

2:42 am on Apr 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Neither the local URL-path nor REQUEST_URI includes the query string, but it is passed through by default -- unless you explicitly replace it with a blank one by appending a question mark to the substitution (new URL).

RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

Note that "." is functionally equivalent here to "!^$" -- it's just shorter/faster.

Jim

MichaelBluejay

7:52 pm on Apr 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Okay, now I don't feel so bad for not figuring it out on my own. Thanks!