A URL can only contain a limited set of characters, yet people search for names with accents, share links with spaces and pass JSON in query strings. Percent-encoding is the system that makes this work. It replaces each unsafe byte with a percent sign and two hexadecimal digits, so a space becomes %20.
This guide explains the rules and the mistakes that cause broken links. You can test every example in the URL encoder and decoder.
How percent-encoding works
Each byte that cannot appear in a URL is written as % followed by its value in hexadecimal. A space is byte 32, which is 20 in hex, so it becomes %20. A percent sign is byte 37, or 25, so a literal % becomes %25.
Characters outside plain ASCII are first converted to UTF-8 bytes, and each byte is encoded. The letter é takes two bytes, so it becomes two escapes:
| Text | Encoded | Why |
|---|---|---|
| a b | a%20b | Space is byte 0x20 |
| 100% | 100%25 | A literal percent is 0x25 |
| café | caf%C3%A9 | é is two UTF-8 bytes, C3 and A9 |
| 你好 | %E4%BD%A0%E5%A5%BD | Each character is three bytes |
Reserved and unreserved characters
The URL standard (RFC 3986) divides characters into two groups. Unreserved characters never need encoding: letters, digits, and the four symbols -, ., _ and ~. Reserved characters have special roles as separators, so they are encoded when you want them to be treated as ordinary data:
: / ? # [ ] @ ! $ & ' ( ) * + , ; =The key point is context. A / separates path segments, so it should stay as it is in a path but be encoded if it is part of a value. A & separates query parameters, so a value that contains one, such as Q&A, must be encoded as Q%26A or the parameter splits in two.
encodeURI versus encodeURIComponent
JavaScript has two built-in functions, and choosing the wrong one is a common bug. encodeURI is for a whole address and leaves the separators alone. encodeURIComponent is for one value and encodes the separators too:
| Input | encodeURI | encodeURIComponent |
|---|---|---|
| a&b=c | a&b=c | a%26b%3Dc |
| x=a/b | x=a/b | x%3Da%2Fb |
| a+b | a+b | a%2Bb |
| café | caf%C3%A9 | caf%C3%A9 |
Use encodeURIComponent for each query-string value you insert into a link. If you pass a value through encodeURI instead, an ampersand or equals sign inside it will break the query. Never run encodeURIComponent on a full URL, because it will encode the colons and slashes too.
Space: %20 or plus?
Both appear in the wild. Percent-encoding uses %20 for a space. HTML forms submitted with the default content type, application/x-www-form-urlencoded, write a space as + instead. The rule is: in a query string created by a form, + means a space, and in a path + is just a plus sign. That is why a literal plus in a value should always be encoded as %2B, as the table above shows.
Double-encoding and other common mistakes
- Encoding twice. A value that was already encoded gets its percent signs encoded again, so
a%20bbecomesa%2520b. If you see%25in a URL where you expected a plain escape, something encoded it twice. - Encoding a whole URL as a component. The colons and slashes become
%3Aand%2Fand the link stops working. - Forgetting to encode user input before placing it in a redirect or link, which can break the link or change its meaning.
- Assuming case matters. The hex digits
%c3%a9and%C3%A9mean the same thing, although uppercase is the convention. - Using the wrong character encoding. Modern systems use UTF-8, but old links may use another encoding and decode as garbled text.
Reading a messy URL
Tracking links and redirects often hide another full URL inside a parameter, encoded so that its own ? and & do not interfere. Decode one layer at a time. The URL parser in our URL tool splits a link into host, path and parameters and decodes each value, which makes it much easier to see where a redirect points.
Method and limits
The encodings in the tables were generated by running the example strings through the standard JavaScript functions, so they match what a browser produces. Other languages and frameworks may differ slightly in which characters they leave unescaped, particularly for the symbols !, *, ', ( and ), so check your library's documentation when exact output matters.
Quick summary
Percent-encoding replaces unsafe bytes with %XX. Use encodeURIComponent for individual values and encodeURI for whole addresses, encode a literal plus as %2B, and watch for double-encoding that turns %20 into %2520.
Try it: Try the URL encoder.
Related tools
- URL Encoder & Decoder: Percent-encode or decode URLs and query strings, and break a URL into its parts.
- Base64 Encoder & Decoder: Encode text to Base64 or decode Base64 back to text, with full Unicode and URL-safe support.
- JSON to CSV Converter: Convert JSON arrays and objects to clean, correctly escaped CSV for Excel or Google Sheets.