apache2 mod_rewrite stripping %2 from URLs?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^example$ http://example.com/my%27problem [r=302,NE,L]
However, Apache (or something) seems to be stripping the %2 from the target, so that I end up with the following.
$ curl -I dbrks.co/example
HTTP/1.1 302 Found
Date: Tue, 09 Jan 2018 16:09:36 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: http://example.com/my7problem
Connection: close
Content-Type: text/html; charset=iso-8859-1
I have many other "RewriteRule" lines, all of which work fine, for example…
RewriteRule ^thanksgiving16/trailhead$ https://www.google.com/maps/place/AMC+Highland+Center+at+Crawford+Notch/@44.2200498,-71.4142477,17z/ [r=302,NE,L]
Gets rewritten exactly as is.
$ curl -I dbrks.co/thanksgiving16/trailhead
HTTP/1.1 302 Found
Date: Tue, 09 Jan 2018 16:17:25 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: https://www.google.com/maps/place/AMC+Highland+Center+at+Crawford+Notch/@44.2200498,-71.4142477,17z/
Connection: close
Content-Type: text/html; charset=iso-8859-1
Any idea whats going wrong?
Thanks,
Dan
3 Replies
Instead, change the flags to [B,r=302,NE,L] so that the "B" escapes the backreference, % in this case.
More details and examples in the docs
I tried your suggestion and changed the rule to add the B flag
RewriteRule ^example$ http://example.com/my%27problem [B,r=302,NE,L]
but the output was still wrong.
$ curl -I dbrks.co/example
HTTP/1.1 302 Found
Date: Tue, 09 Jan 2018 20:51:40 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: http://example.com/my7problem
Connection: close
Content-Type: text/html; charset=iso-8859-1
However, when I manually escaped the the URL like so
RewriteRule ^example$ http://example.com/my\%27problem [r=302,NE,L]
then it produces the correct output.
$ curl -I dbrks.co/example
HTTP/1.1 302 Found
Date: Tue, 09 Jan 2018 20:53:50 GMT
Server: Apache/2.4.18 (Ubuntu)
Location: http://example.com/my%27problem
Connection: close
Content-Type: text/html; charset=iso-8859-1
Manually escaping the % signs is a good enough work around for me, but I wish I understood why the B flag did not have the same effect.
Another alternative is to use double quotes around the url without any escaping.