Mod rewrite trouble with question mark "?"

Hello friends,

So far I’ve been able to manage with mod rewrite rules for basic URL beautifying such as hiding regular browser GET requests, etc … This time I got into trouble because a question mark in the URL leads me to a 404 error.

I have this rule:

RewriteRule ^cat-(.*)$ index.php?cat=$1 [L]

which makes my URL look like this:

http://mysite.com/cat-somecategory

Everything good so far but occasionally, to the query string is also passed a sort parameter like:

http://mysite.com/cat-somecategory?s=title:desc

and want the rule:

RewriteRule ^cat-(.*)\\?s=(.*)$ index.php?cat=$1&s=$2 [L]

Even I’m escaping the question mark, I still get the 404 page. If I am replacing the question mark with any other character, the rewrite rule works! I need it to work with the category rule alone as well with the sort parameter since it’s optional but cannot get there no matter what!

I would appreciate any advice you might have.

Thanks,
Rem.

rem,

How unusual! Two threads with the same problem: Attempting to use reserved characters. Try a read of the techno-babble of [URL=“http://www.ietf.org/rfc/rfc2396.txt”]Uniform Resource Identifiers (URI): Generic Syntax
and you’ll find that ? is a reserved character. Because its SOLE purpose in a URI is to separate the URI from the query string, it MUST be encoded to be used at all (%3f).

Now, I don’t think that’s your REAL problem as mod_rewrite separates the {QUERY_STRING} from the {REQUEST_URI} string, too. Because a RewriteRule can ONLY examine the {REQUEST_URI} string, you MUST use a RewriteCond to examine the {QUERY_STRING}.

If you need more info, I refer you to the tutorial article (with coding examples) linked in my signature.

Regards,

DK

Thank you DK, that makes sense. Ok, will read your article and hopefully it’ll put more light on this issue.

Best,
rem.

You could try:

RewriteRule ^cat-(.*)$ index.php?cat=$1 [L,QSA]

QSA = Query String Append, so it will append any query string onto the end of the URL instead of ignoring it.

Thanks, I did tried that, but obviously there is something I’m doing wrong so it didn’t work. I had to re-think the whole rule set in order to pass this problem…

What if I there are several atoms and there isn’t just an order throughout the site?

ex:
display.php?country=$1&state=$2&city=$3

can also be
display.php?state=$1&city=$2&country=$3

or
display.php?city=$1

With a number higher than 3 atoms, there are already lots of combinations, I am sure there gotta be an easier and logical way around this… Please help.

Thanks!

rem,

QSA doesn’t care about the order of the key=value pairs in the query string, any existing string is merely copied rather than get destroyed by the new query string entry.

Regards,

DK

Thanks!