Apache   Escaping characters on Apache rewriting rules



If you use Apache, probably you have used the mod_rewrite and the RewriteRule sentences.

One simple rewriting rule:

RewriteRule ^/cars    http://myweb.com?productid=244 [L,R]

So, when I try to access to subfolder "cars" of the configured virtual host, the browser will be redirected to http://myweb.com?productid=244

But, what if the product id contains, for example, a character like '%'?Let's try:

RewriteRule ^/cars    http://myweb.com?productid=2%44 [L,R]

 

The browser will be redirected to…  http://myweb.com?productid=24 ???

The problem is that '%' is an special character in Apache and transforms the '%44' sequence into '4'. To avoid that "transformation" we can tell apache not to interpret the '%' as an especial character.

RewriteMap ESCAPE int:unescape
RewriteRule ^/cars/(.*) http://myweb.com?productid=2${ESCAPE:%}44 [NE,L]

 

Now the final URL is… http://myweb.com?productid=2%44   OK!

 

Leave a Reply

Your email address will not be published. Required fields are marked *