I have a url containing the following:
/somepath/morestuff/ohno%25foobar
For some reason apache is reporting a 400 bad request (it has something to do with the %25). I am using mod_rewrite to rewrite the path to point to my codeigniter instance but it’s not even getting to codeigniter, it’s just the default apache error.
Any ideas?
I suspect that you’re using
PATH_INFOto handle your CodeIgniter requests. Consequently, your.htaccessfile contains a rule set that looks similar to this:When
mod_rewritetests your URLs, they have already been decoded to their natural character format, so in this case%25has become just%. When you apply this rule, the backreference actually contains the literal textsomepath/morestuff/ohno%foobar, which is not re-encoded by default. Apache has no idea what that%is doing in your request path to/index.php/somepath/morestuff/ohno%foobarand chokes, giving you that error.If you’re running Apache 2.2,
mod_rewriteadded theBflag for this purpose, allowing you to automatically escape backreferences rewritten to your URL. Adding it to your current flag list should fix the problem in that case:There’s also an
escapeRewriteMapthat’s available as an internal map in previous Apache versions ofmod_rewrite, but unfortunately this map has to be enabled at the server or virtual server configuration level, so may not be available if you’re running your site on shared hosting. It does the same thing, though a bit more deliberately.In your server/virtual server configuration:
Then, wherever you define your rules:
Keep in mind that CodeIgniter doesn’t need to use
PATH_INFOto get the request information, and usingREQUEST_URIis perfectly acceptable here if you aren’t usingmod_rewriteto do any other transformations (and would avoid this headache altogether). I think by default CodeIgniter is set to get the request fromAUTO(assuming I haven’t gotten my frameworks mixed up), so simply not rewriting the request to the URL with path info would be enough to make that change.