Apache not decoding %3F – causes 404 errors

This happens on only one of my servers, and I haven't been able to nail down the problem. It feels like a configuration related issue, yet I can't reveal any differences between the working and non-working configuration.

An example URL:

http://www.mysite.com/test.html%3Fparam=1

In a working configuration, the server has no problem redirecting the above URL to:

http://www.mysite.com/test.html?param=1

In a non-working configuration, Apache simply returns a 404 error. Not cool!

After scouring the web for a few hours, i came across a knowledgeable fellow who really put a dent in this problem. I wish I could remember the link, to give him proper credit.

Essentially, he created two Rewrite Conditions to locate these encoded characters, allow us to Rewrite appropriately:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?\ ]*\?[^%\ ]*\%(25)*(3[Dd]|26)[^\ ]*)\ HTTP/ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(([^%\ ]*\%(25)*([^3].|.[^Ff]))*[^%\ ]*\%(25)*3[Ff][^\ ]*)\ HTTP/

The one missing link was a proper RewriteRule. I cam up with the below, which seems to work in all of my test cases. Essentially, replacing the %3F with a ?.

RewriteRule ^([^\%]+) http://www.mysite.com$1 [R=301,L]

Again, I have a feeling this is a terribly hacky approach to a simple Apache misconfiguration. But, for those of use who can't Google 50 search terms per minute to find a solution, the above is pretty close!

Why follow me on Twitter?

  • I tweet about new technologies, services or libraries I find interesting
  • Yeah, sometimes I'll post a pet-peeve or rant about something trivial
  • If I discover something that made my web development life easier, I share it
  • I'll shout out any handy tip that I think might be useful to other devs


Tagged , .

Updated: 2011-12-23

Phil LaNasa follow us in feedly