Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need to encode URLs?

Tags:

url

Why do you need to encode urls? Is there a good reason why you have to change every space in the GET data to %20?

like image 899
Brian Avatar asked Jan 28 '10 06:01

Brian


People also ask

How is URL encoded?

All characters to be URL-encoded are encoded using a '%' character and a two-character hex value corresponding to their UTF-8 character. For example, 上海+中國 in UTF-8 would be URL-encoded as %E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B .

Why is %20 used in URLs?

A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.


2 Answers

Because some characters have special meanings.

For instance, in a query string, the ampersand (&) is used as a separator between key-value pairs. If you were to put an ampersand into one of those values, it would look like the separator between the end of a value and the beginning of the next key. So for special characters like this, we use percent encoding so that we can be sure that the data is unambiguously encoded.

like image 73
Jim Avatar answered Sep 17 '22 11:09

Jim


From RFC 2936, section 2.4.3:

The space character is excluded because significant spaces may disappear and insignificant spaces may be introduced when URI are transcribed or typeset or subjected to the treatment of word- processing programs. Whitespace is also used to delimit URI in many contexts.

like image 27
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 11:09

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!