RewriteRule question mark escape

Helicon Ape provides support for Apache .htacces and .htpasswd configuration files for Microsoft IIS.
User avatar
Posts: 22
Joined: 19 Aug 2012, 20:24

RewriteRule question mark escape

28 May 2013, 18:52

Hi,

Does a question mark in RewriteRule need to be escaped? It seems so.

This started as a problem trying to remove one querystring parameter, and leave the rest, and redirect. (This is a small piece of a bigger problem; I found questions on stackoverflow, but no answer yet.)

rewrite
/something/?x=abc&y=def
to
/something/abc/?y=def

To simplify, let's say x parameter is always first, and there are always additional parameters.
I have this:

Code: Select all
#  Starts with x=, non-ampersand, ampersand, remaining required.
RewriteCond %{QUERY_STRING} ^x=([^&]+)&(.+)
RewriteRule ^/something/$ /something/%1/?%2 [NC,R=301,L]


The problem is I get the entire querystring, instead of just portion in the %2 group:
/something/abc/?x=abc&y=def

After trying several things, I found if I escape the question mark, then it "knows" it has a querystring, and won't append the original:
This works:
Code: Select all
#  Starts with x=, non-ampersand, ampersand, remaining required.
RewriteCond %{QUERY_STRING} ^x=([^&]+)&(.+)
RewriteRule ^/something/$ /something/%1/\?%2 [NC,R=301,L]


Thank you,
Bob

User avatar
Posts: 22
Joined: 19 Aug 2012, 20:24

Re: RewriteRule question mark escape

28 May 2013, 19:46

To clarify, my question is:

Does a question mark in RewriteRule Substitution need to be escaped?

Thanks,
Bob

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: RewriteRule question mark escape

28 May 2013, 21:58

Hello,

In your particular case - yes, it's etter to escape it.
In general, to avoid appending original query string you need to end the rule with '?' like 'RewriteRule a /b? [NC,L]'

Regards
Andrew

User avatar
Posts: 22
Joined: 19 Aug 2012, 20:24

Re: RewriteRule question mark escape

29 May 2013, 11:55

Hi,

Thanks for the reply. To clarify though, I'm not trying to suppress the entire querystring. In fact, if I put the ? on the end, it removes all querystring parameters, even the ones I put in manually.

The question is how to put in my own querystring parameters, and have it omit the originals.

This removes all:
RewriteRule a /b? [NC,L]

This includes them all (even though I only want 'x')
RewriteRule a /b?x=123 [NC,L]

This works as I expected, including 'x' and no others.
RewriteRule a /b\?x=123 [NC,L]


The question is to confirm whether I MUST always escape the question mark, not just that it's better to. Or, is this a bug that needs a fix, if you don't mean to require it?

Thanks,
Bob

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: RewriteRule question mark escape

29 May 2013, 13:20

Question mark is a symbol used in regular expression and has meaning behind it. However, usually it doesn't require any escaping. I don't use escaping.

Here are some correction to your examples:

Code: Select all
This removes any querystring:
RewriteRule a /b? [NC,L]

Add X parameter to B and appends the original querystring from A
RewriteRule a /b?x=123 [NC,L]

Add X parameter to B and DOESN'T append the original querystring from A
RewriteRule a /b?x=123? [NC,L]


Regards
Andrew

User avatar
Posts: 22
Joined: 19 Aug 2012, 20:24

Re: RewriteRule question mark escape

29 May 2013, 13:30

This is good to track down. When I do the third one, as shown without escaping it:

Code: Select all
Add X parameter to B and DOESN'T append the original querystring from A
RewriteRule a /b?x=123? [NC,L]


It removes the both the X parameter and the original querystring, producing just /b

I'm using version 0105, but I believe I saw this also on 0084 (the previous one I was using).

Thanks,
Bob

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: RewriteRule question mark escape

29 May 2013, 13:43

I'll look into it. thanks

Return to Helicon Ape

Who is online

Users browsing this forum: No registered users and 0 guests