Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule - trouble with question mark

         

barells

6:03 pm on Jul 21, 2007 (gmt 0)

10+ Year Member



Hi,

I am having trouble with my .htaccess rewrite rule, where I can't seem to properly escape the question mark in the url I want to redirect.

Here is my example:

RewriteRule ^report2\.html\?loc=wna_us_fl_cape_canaveral$

/surf-forecast/cape-canaveral-florida.html [R=301,L]

The problem area is the .html\?loc=... If I take away the questions mark then it works fine and the escape character doesn't seem to work in this example.

Any help here? Much appreciated.

jdMorgan

6:14 pm on Jul 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not part of a URL; They are data attached to a URL to be passed to the resource (e.g. script) at that URL.

You must use

RewriteCond %{QUERY_STRING} ^loc=wna_us_fl_cape_canaveral$ 

along with a URL pattern in the RewriteRule, to test (and/or back-reference) your query-string.

Jim

barells

7:17 pm on Jul 21, 2007 (gmt 0)

10+ Year Member



I see.

this is what i did:

RewriteCond %{QUERY_STRING} ^loc=wna_us_fl_cape_canaveral$
RewriteRule ^report2\.html$ /surf-forecast/cape-canaveral-florida.html [R=301,L]

This does the redirect, however, the query string gets appended on to the new url. How do I get rid of the query string. Perhaps this is what you were referring to with the testing / back referencing? I'm not sure how to go about that.

barells

7:24 pm on Jul 21, 2007 (gmt 0)

10+ Year Member



I found an answer to my question about removing the appended query string.

I put a "?" on the end of the url i was redirecting to, and this removed the query string from the new(redirected) url.

bbunlock

7:54 pm on Jul 21, 2007 (gmt 0)

10+ Year Member



cant believe I just found that out, used RewriteRule ages ago when doing a site and all/most old urls had? & = in them, when redirected they still worked but were ugly as they appended that cod tot he end of the url, and now I find out all I had to do was add a? to the end of the destination url

well at least I know for next time (very soon actualy), thanks for the info