URL Encoding Explained (Percent-Encoding and When It Applies)
URLs are surprisingly fussy about which characters they will accept. Spaces, accents, and symbols can break a link or corrupt a query parameter unless they are encoded. This guide explains percent-encoding, the difference between `%20` and `+`, and how to fix a broken URL.
Why must a URL be encoded?
A URL is made of reserved characters (/, ?, &, =, #, and others) that carry structure. If your data contains one of them, you must encode it so the browser does not misread it. Non-ASCII characters like ? and é must also be encoded into percent sequences.
How does percent-encoding work?
Each unsafe byte becomes a % followed by two hexadecimal digits. The letter ? becomes %3F, é becomes %C3%A9, and many spaces appear as %20. Because these hex strings never collide with reserved characters, the URL stays unambiguous.
Why do some URLs use %20 and others use +?
In the path portion of a URL, spaces are normally encoded as %20. In application/x-www-form-urlencoded bodies and some query strings, a + is used for a space instead. The choice depends on context, which is why one correct-looking URL can still render differently.
What are common URL breakages and how do you spot them?
A link with a raw & in a value silently splits the parameter. A # in a value truncates the rest of the URL. A value with spaces may be cut off. If your data contains any of these, encode it before building the link.
How do you encode and decode a URL safely?
Encode the right part of the URL with the right function and verify the result. The URL encoder lets you switch between %20 and + styles, detects what is already encoded, and handles whole queries or individual components.