Why URLs Need Encoding
URLs can only contain a limited set of characters from the ASCII character set. When you need to include special characters, spaces, or non-ASCII characters in a URL, they must be encoded.
Reserved Characters
These characters have special meaning in URLs:
How Percent-Encoding Works
Each unsafe character is replaced with % followed by its hexadecimal ASCII value:
- Space →
%20
& → %26
= → %3D
? → %3F
Example
Original: https://example.com/search?q=hello world&lang=en
Encoded: https://example.com/search?q=hello%20world&lang=en
Common Gotchas
- Spaces: Can be
%20 or + (in query strings only)
- Double encoding: Encoding already-encoded strings creates bugs
- UTF-8: Non-ASCII characters are first converted to UTF-8 bytes, then percent-encoded