当URL包含%字符时使用Apache重写模块向每个%添加“52”

I need to modify the path of a long URL which includes % characters in it, for example:

https://www.my-geoserver.co.uk/geoserver/HNT_Demo/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&LAYERS=HNT_Demo%3ANO2_Wed_h18_WGS&TILED=true&login=admin&WIDTH=256&HEIGHT=256&CRS=EPSG%3A3857&STYLES=&BBOX=-132083.18487678468%2C6780270.1570082735%2C-127191.2150665334%2C6785162.126818525

When i attempt to use Apache rewrite module, I notice that the % characters are causing problems, so for example the section here:

image%2Fpng

Upon rewrite becomes (notice 52 has appeared):

image%252Fpng

%2F is the URL Encoded value of a forward slash, so I can't believe this is an uncommon problem.

The code I'm using (for experimental purposes) is:

RewriteCond %{QUERY_STRING} ^(.*)login=[^&]*(?:&(.*)|)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=301,L]

Which should simply remove the login variable which it does. But I also get a modification of the % values throughout the URL, where in all cases 52 is appearing which I don't think is related to the RewriteRule per say.

Is there a way to allow RewriteRule to work with % values in the URL? I have tried the following flags (somewhat desperately):

  • NC
  • NE
  • QSA

But with no luck.

Additional:

I've just found this very old bug report which shows the same behavior of % being 'escaped' to %25. Is this possibly a bug?

https://bz.apache.org/bugzilla/show_bug.cgi?id=39746

This bug is referring to using a reverse proxy and the error being in mod_proxy, well I'm using a reverse proxy to host Tomcat, could this be related?

Additional 2:

The answer to a question here:

the auto urldecode in apache mod_rewrite

Suggests using server variables to get around the % issue, unfortunately, I already am using them. I tried replacing QUERY_STRING with REQUEST_URI and THE_REQUEST, both of which result in the same behaviour:

RewriteCond %{THE_REQUEST} ^(.*)login=[^&]*(?:&(.*)|)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=301,L]