Complete Guide to URL Encoding and Decoding
In this guide
URLs can only contain a limited set of characters. Anything else, such as spaces, accented letters, or an ampersand inside a value, has to be written in a safe form called percent-encoding. Getting it wrong breaks links, corrupts search queries and causes puzzling bugs in forms and APIs.
How percent-encoding works
Each unsafe byte is replaced with a percent sign followed by two hexadecimal digits. A space becomes %20, an ampersand becomes %26 and a question mark becomes %3F. Characters outside ASCII, like é or 你, are first converted to UTF-8 bytes and then each byte is encoded, so é becomes %C3%A9.
Reserved characters
Some characters have jobs in a URL: the question mark begins the query, the ampersand separates parameters, the equals sign separates a name from its value, the hash begins a fragment and the slash separates path parts. If one of these appears inside a value, it must be encoded or the URL will be read differently from what you intended.
encodeURI versus encodeURIComponent
In JavaScript, encodeURI is meant for a whole URL and leaves the structural characters alone. encodeURIComponent is meant for a single piece, such as one query parameter value, and encodes the reserved characters as well. Using the first on a value is a classic bug that lets ampersands break your query.
The plus sign and spaces
In form-encoded query strings a plus sign stands for a space, while in the path part of a URL a plus is a literal plus. Mixing these up gives you phrases with unexpected plus signs or missing spaces.
Decoding and double encoding
Decoding turns the percent codes back into the original characters. If you see %2520 in a URL, the value was encoded twice (the %25 is an encoded percent sign). Decode once at a time and compare the results to find where the extra encoding happened.
Worked example: encoding and double encoding
The text hello world & café? went through the URL Encoder as hello%20world%20%26%20caf%C3%A9%3F: the spaces became %20, the ampersand %26, the question mark %3F and the accented e the two bytes %C3%A9. Decoding hello%20world%26x returned hello world&x.
Encoding something that is already encoded breaks it. Feeding hello%20world back into the encoder produced hello%2520world, because the percent sign itself became %25, and a server that decodes once will then see %20 as literal text. Encode raw values once, as late as possible.
Key Takeaways
- Unsafe bytes become %XX in hexadecimal.
- Encode each parameter value, not the whole URL structure.
- Plus means space only in form-encoded queries.
- %25 in a URL often signals double encoding.
Frequently Asked Questions
Why does a space become %20?
A space is not allowed in a URL, and 20 is its hexadecimal ASCII code.
Should I encode an entire URL?
No. Encode the pieces that hold data, such as query values. Encoding the whole URL would also encode the slashes and colon.
How do non-English characters work?
They are converted to UTF-8 bytes and each byte is percent-encoded.
Free tools mentioned in this guide
- URL Encoder & Decoder
- URL Path Sanitizer
- URL Parameter Parser
- UTM Campaign Generator
- Slug Generator
- HTML Entity Encoder
Browse every Developer Tools utility, or see all tools.