Why do you need to encode URLs?

Url

Url Problem Overview


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?

Url Solutions


Solution 1 - Url

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.

Solution 2 - Url

From http://www.ietf.org/rfc/rfc2396.txt">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.

Solution 3 - Url

  • 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.

Solution 4 - Url

Let's break down your question.
Why do you need to encode URL?
A URL is composed of only a limited number of characters and those are digits(0-9), letters(A-Z, a-z), and a few special characters("-", ".", "_", "~").
So does it mean that we cannot use any other character?
The answer to this question is "YES". But wait a minute, there is a hack and the hack is URL Encoding or Perchantage Encoding. So if you want to transmit any character which is not a member of the above mentioned (digits, letters, and special chars), then we need to encode them. And that is why we need to encode "space" as "%20".
OK? Is this enough for URL encoding? No this is not enough, there's a lot about URL encoding but here, I'm not gonna make it a pretty big, boring technical answer. But If you want to know more, then you can read it from here: https://www.urlencoder.io/learn/ (Credit goes to this writer)

Solution 5 - Url

Well, you do so because every different browsers knows how the string that makes up the URL is encoded. converting the space to %20, etc makes that URL/URI portable. It could be latin-1 it could be unicode. It needs normalized to something that is understood universally. Take a look at rfc3986 https://www.rfc-editor.org/rfc/rfc3986#section-2.1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrianView Question on Stackoverflow
Solution 1 - UrlJimView Answer on Stackoverflow
Solution 2 - UrlIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 3 - UrlJoeView Answer on Stackoverflow
Solution 4 - Urlnovice2ninjaView Answer on Stackoverflow
Solution 5 - UrlbrianrayView Answer on Stackoverflow