Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache: %25 in url (400 Bad Request)

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?

like image 370
Ken Struys Avatar asked Aug 23 '10 16:08

Ken Struys


People also ask

How do you fix bad request your browser sent a request that this server could not understand?

Clear your browser cache Clearing your Chrome cache and cookies can help you to fix bad request errors. As you visit different websites, your browser stores temporary files, and various scripts in order to load the respective websites faster next time.

How do I fix HTTP error 400 a request header field is too long?

Chosen solutionClear the Cache and remove the Cookies for websites that cause problems via the "3-bar" Firefox menu button (Options/Preferences). If clearing cookies didn't help then it is possible that the cookies. sqlite file in the Firefox profile folder that stores the cookies got corrupted. rename/remove cookies.

What does 400 that's an error mean?

A 400 Bad Request Error occurs when a request sent to the website server is incorrect or corrupt, and the server receiving the request can't understand it. Occasionally, the problem is on the website itself, and there's not much you can do about that.


3 Answers

I suspect that you're using PATH_INFO to handle your CodeIgniter requests. Consequently, your .htaccess file contains a rule set that looks similar to this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php/$0 [L]

When mod_rewrite tests your URLs, they have already been decoded to their natural character format, so in this case %25 has become just %. When you apply this rule, the backreference actually contains the literal text somepath/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%foobar and chokes, giving you that error.

If you're running Apache 2.2, mod_rewrite added the B flag 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:

RewriteRule ^.*$ index.php/$0 [B,L]

There's also an escape RewriteMap that's available as an internal map in previous Apache versions of mod_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:

RewriteMap escape int:escape

Then, wherever you define your rules:

RewriteRule ^.*$ index.php/${escape:$0} [L]

Keep in mind that CodeIgniter doesn't need to use PATH_INFO to get the request information, and using REQUEST_URI is perfectly acceptable here if you aren't using mod_rewrite to do any other transformations (and would avoid this headache altogether). I think by default CodeIgniter is set to get the request from AUTO (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.

like image 132
Tim Stone Avatar answered Oct 02 '22 09:10

Tim Stone


Apache 2.2.12+, just use the B flag. Otherwise, see here:

http://www.dracos.co.uk/code/apache-rewrite-problem/

like image 37
philfreo Avatar answered Oct 02 '22 09:10

philfreo


In jquery before setting to url you have to encode like this this will encode % and / both..... "encodeURI(encodeURI(encodeURIComponent('your string')));"

on php or view page you have to decode like following

urldecode(urldecode(htmlspecialchars('your encoded string', ENT_QUOTES)))

like image 23
chandresh Avatar answered Oct 02 '22 11:10

chandresh