URL Encoding Explained: Percent-Encoding Special Characters
URL encoding (officially percent-encoding, defined in RFC 3986) is how characters that are not safe inside a URL get represented. A URL can only legally contain a limited set of characters. Everything else — spaces, non-ASCII text, and reserved symbols — is written as %XX, where XX is the hexadecimal byte value of the character.
Common percent-encoding reference
| Character | Encoded | Character | Encoded |
|---|---|---|---|
| space | %20 | # | %23 |
| ! | %21 | & | %26 |
| " | %22 | + | %2B |
| $ | %24 | / | %2F |
| % | %25 | : | %3A |
| < | %3C | = | %3D |
| > | %3E | ? | %3F |
| @ | %40 | , | %2C |
Letters, digits, and the characters - . _ ~ never need encoding. Percent-encoding is case-insensitive for the letters: %2F and %2f are equivalent.
Non-ASCII text: UTF-8 first
Non-ASCII characters are encoded as their UTF-8 byte sequence, one %XX per byte. The Chinese text 你好 is three bytes per character in UTF-8, so it becomes six percent sequences:
你好 → %E4%BD%A0%E5%A5%BD
encodeURIComponent vs encodeURI (JavaScript)
JavaScript offers two functions, and mixing them up is the classic bug:
// Encodes everything except A-Z a-z 0-9 - _ . ! ~ * ' ( )
encodeURIComponent("a b&c=d"); // "a%20b%26c%3Dd"
// Leaves the URL structure characters intact
encodeURI("https://example.com/a b"); // "https://example.com/a%20b"- Use
encodeURIComponentfor query string values, path segments, and anything you build dynamically. - Use
encodeURIonly when you have a whole URL and want to keep:// ? & =working. - Never pass user input into a URL without encoding — a stray
&or#silently changes what the URL means.
When you need URL encoding
- Building query strings:
?q=hello+worldor?q=hello%20world. - Sending form data or JSON in a GET request.
- Embedding user-generated content in links.
- Debugging a URL that broke because a value contained a reserved character.
encodeURIComponent semantics, entirely in your browser.FAQ
What is %20 in a URL?
%20 is the percent-encoded form of the space character. A space cannot appear literally in a URL, so it is written as %20 (the hexadecimal value of the space byte, 0x20).
When should I use encodeURIComponent?
Use encodeURIComponent when building query string values, because it encodes reserved characters such as &, =, and #. Use encodeURI only for a whole URL where you want to keep its structure intact.
Does URL encoding work for Chinese characters?
Yes. Non-ASCII text is first encoded as UTF-8 bytes, and each byte becomes a percent sequence. For example, 你好 becomes %E4%BD%A0%E5%A5%BD.