1

I need help regarding a requirement. We have Apache 2.4.6 installed

I have a URL which has an encoded query string:

https://example.com/home?testStr%3Dhello%26id%3Drad

I am trying to find out how to double encode only the query string part of the URL using mod_rewrite in Apache webserver to:

https://example.com/home?testStr%253Dhello%2526id%253Drad

This is for one of the issues I am trying to fix.

  • Just to confirm, you would like to have the query string to be double encoded? – pacey Oct 2 '18 at 7:19
  • might I ask WHY you are trying to do this? Why use encoded strings at all, that's a really ugly URL. – Broco Oct 2 '18 at 7:40
  • @Broco I cannot avoid this.. this request is sent by 3rd party and we get the request and validate and forward the request.. but there is a bug one of the product and we cannot apply patch as of now.. Our alternative is to double encode the query string... – rad123 Oct 2 '18 at 14:53
  • @pacey - yes i need to double encode only query string part.. not the request URL.... – rad123 Oct 2 '18 at 14:54
  • I'm sorry but I still have issues comprehending. You have a web application, which requires the urls to be urlencoded (but only once), you have a third party accessing this application - with query strings urlencoded twice? Or is it the other way around: third party sends the query string urlencoded once and your application expects double-urlencoded query strings? – pacey Oct 2 '18 at 15:19
1

Just to echo the concerns in comments... this is a strange requirement. However, you can do this using mod_rewrite. For example, try the following:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(testStr%3Dhello%26id%3Drad)$
RewriteRule ^/?(home)$ /$1?%1 [B,NE,R,L]

This issues a temporary (302) redirect from /home?testStr%3Dhello%26id%3Drad to /home?testStr%253Dhello%2526id%253Drad (doubly encoded).

The QUERY_STRING server variable is not %-decoded, so you match against the literal query string as given in the RewriteCond directive.

The $1 backreference refers to the captured subpattern in the RewriteRule pattern ie. home.

The %1 backreference refers to the captured subpattern in the last matched CondPattern (the RewriteCond directive) ie. the %-encoded query string (testStr%3Dhello%26id%3Drad).

The B (escape backreferences) flag on the RewriteRule directive %-encodes the backreference (%1) to effectively doubly-encode the query string.

The NE (noescape) flag prevents the susbstitution being further (triple) URL-encoded!

| improve this answer | |
  • How did you get on with this? – MrWhite Oct 4 '18 at 9:46
  • Hi Sorry.. this didn't work.. the regex was not picked up by webserver.. – rad123 Oct 7 '18 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.