URL Encoding Explained: Why Spaces Become %20
URLs can only contain a limited set of characters. Anything else, such as a space, an ampersand inside a value or an accented letter, must be percent-encoded: replaced by a percent sign and two hexadecimal digits for each byte. A space becomes %20, an ampersand becomes %26 and é becomes %C3%A9.
Why encoding is needed
Some characters have special jobs in a URL:
| Character | Job in a URL | Encoded |
|---|---|---|
| ? | Starts the query string | %3F |
| & | Separates parameters | %26 |
| = | Separates a name from its value | %3D |
| # | Starts the fragment | %23 |
| / | Separates path parts | %2F |
| space | Not allowed | %20 |
If a search term contains an ampersand, such as "salt & pepper", leaving it unencoded would split the value in two.
Example
"hello world & tools" encodes to hello%20world%20%26%20tools. The URL encoder and decoder encodes a component or decodes it back. Decoding caf%C3%A9%20%26%20bar gives "café & bar".
Non-English characters
Letters outside basic ASCII are first converted to UTF-8 bytes, then each byte is encoded. é is two bytes in UTF-8, so it becomes %C3%A9. Browsers often display the readable form in the address bar but send the encoded form.
%20 or +?
In the query string of a form submission, spaces are often written as + instead of %20. Most servers accept both in query strings, but + means a literal plus sign in the path. When in doubt, %20 is the safe choice.
Reading and building query strings
The URL query string parser splits a URL into its parameters and decodes them: https://example.com/search?q=red+shoes&size=9 gives q = red shoes and size = 9. The URL query string builder does the reverse, encoding each value for you.
Encode components, not whole URLs
Encode each value separately, not the full URL, or the ? and & that structure the link get encoded too. Tools such as the UTM link builder handle this automatically when they add parameters.
Questions and answers
What does %20 mean in a URL?
It is an encoded space. URLs cannot contain spaces, so each one is written as %20.
What is the difference between encodeURI and encodeURIComponent?
In JavaScript, encodeURI leaves characters such as ? and & alone so a full URL still works; encodeURIComponent encodes them, for use on single values.