AHmedRef
AHmedRef

Reputation: 2611

400 code error when URL contains % symbol? (NGINX)

How to prevent a server from returning an error 400 code error when the URL contains % symbol using NGINX server?

Nginx configuration for my website:

....
   rewrite ^/download/(.+)$   /download.php?id=$1  last;
....

When I tried to get access to this URL: http://mywebsite.net/download/some-string-100%-for-example I got this error:

400 Bad Request

With this url : http://mywebsite.net/download/some-string-%25-for-example it's work fine !

Upvotes: 1

Views: 4030

Answers (1)

Aminah Nuraini
Aminah Nuraini

Reputation: 19166

It's because it needs to be URL encoded first.

This will explain: http://www.w3schools.com/tags/ref_urlencode.asp

URLs can only be sent over the Internet using the ASCII character-set.

Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

The URL interpreter is confused to see a % without hexadecimals after it.

Why would you think of solving by changing Nginx configuration???

It's impossible to solve from the server side. It's a problem from the client side.

https://headteacherofgreenfield.wordpress.com/2016/03/23/100-celebrations/

In that URL, the title is 100% Celebrations! but the permalink is autogenerated to 100-celebrations. It's because they know putting 100% will cause a URL encode problem.

If even Wordpress doesn't do it your way, then why should you do it?

Upvotes: 2

Related Questions