Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule and new Querystring from RewriteMap

Question mark inserted by RewriteMap does not stop original Querystring

         

BartBogdan

2:46 pm on Feb 8, 2012 (gmt 0)

10+ Year Member



Hello all,

I have a RewriteRule where a RewriteMap inserts a new query string into the substitution string. Even though this RewriteMap inserts a question mark into the substitution string, the original query string is still appended to the new request. I can stop this by adding an additional question mark to the end of the substitution, but then I end up with a request that contains two question marks.

Is there any other way to stop the original query string from being appended? Or is there any way to make the RewriteRule recognise that I am inserting a question mark with the RewriteMap? Maybe by chaining multiple rules?

Any thoughts are welcome.

Regards,
Bart

lucy24

10:25 pm on Feb 8, 2012 (gmt 0)

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



Start by showing us your actual code. Does the rewrite only involve the query string? If so, the target should say

... http://www.example.com/?{stuff from map here} [R=301,L]

and the map should not say anything about a question mark.

Make sure you understand exactly how query strings are handled by mod_rewrite. There is a long piece of boilerplate that gets pasted into this forum every few weeks; a Forums search should bring it up.

BartBogdan

11:58 am on Feb 9, 2012 (gmt 0)

10+ Year Member



Thanks for your reply.

Basically what I'm facing is that the complete request string for another server is percent encoded as a get parameter in the incoming request. To handle this I currently have the following code:

RewriteMap percdec int:unescape
RewriteCond %{QUERY_STRING} query=(.+?)
RewriteRule / http://otherserver.com/${percdec:%1}? [P]


The incoming GET parameter 'query' can be something like index.php?page=12 (after decoding). So the question mark is part of this string.

Probably I will need to add a second RewriteCond that splits up the %1 from the first RewriteCond in two parts, the part before the question mark and the part after the question mark.

lucy24

9:49 pm on Feb 9, 2012 (gmt 0)

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



Does this part

${percdec:%1}?

work? It looks like something that would send my server into a 500 tizzy.

The form

query=(.+?)

also looks very unnerving. Normally if you're looking for a specific query you'd have something like

paramname=([^&]+)

to pick up exactly one item. Is the "query=blahblah" ever followed by additional parameters that you also need to pick up? Is it always the first item in your inherited query string, or can there be things before it that you're throwing away?

I kinda think the whole code needs to be turned upside-down and backward. Start by looking for %3F in the query string. Then cut out everything that comes after it, and feed that to the map.

... and if the original query string ever contains %25-anything, meaning that it has been percent-encoded more than once, tear up the whole thing and start over.