Why do you need to encode URLs?

  • originally older browsers could get confused by the spaces (not really an issue anymore).
  • now, if someone copies the url to send as a link - the space can break the hyperlink - ie

Hey! Check out this derping cat playing a piano!

http://www.mysite.com/?video=funny cat plays piano.

See how the link breaks?

Now look at this:

http://www.mysite.com/?video=funny%20cat%20plays%20piano.

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.


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.

Tags:

Url