Community

apache2 mod_rewrite stripping %2 from URLs?

Avatar forum:k2solinode 6 months, 1 week ago

I have the following in my .htaccess file

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

Since the % is reserved for backreferences, you are can't use it like that.

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

Interesting, thank you for the reply. It was very helpful, as back reference does seem to be the problem!

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.

I don't know why the B didn't work, in theory it should have, but most probably I'm missing something from the spec.

Another alternative is to use double quotes around the url without any escaping.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct