How to stop error 400 when URL contains a percent

2019-05-07 06:56发布

问题:

Is there a simple way to prevent a server from returning an error 400 (bad request) when a URL is requested containing a percent % symbol?

Many major sites have this same problem, for example: http://twitter.com/% or http://bing.com/%

But other sites have fixed this and return a custom error page instead, rather than the standard server response, such as: http://facebook.com/% or http://google.com/%

Is there a simple, single way to escape a single character % symbol from returning a 400 error, but instead, returning a 404 error using a custom error document using mod_rewrite or similar in Apache?

回答1:

Is there a simple, single way to escape a single character % symbol from returning a 400 error

Yes, you have to urlencode any characters you put in a url, if they are not expected to have a special meaning.

For example in PHP:

$string = urlencode('%');

FYI % is encoded as %25: https://twitter.com/%25



回答2:

I solved this by making two things. Using the apache configuration i directed this error to my 400.shtml document. Second thing was in my .htaccess file:

RewriteEngine On
RewriteRule 400.shtml 404.shtml [L]

You can play with those two and use your serverside script to get this error handled instead just 404.shtml.

Cheers.