URL Encoding Explained: Percent-Encoding and Reserved Characters
- What percent-encoding does
- Why reserved characters need escaping
- Encoding a whole URL vs encoding a single value
- Encoding a value (encodeURIComponent)
- Encoding a whole URL (encodeURI)
- A worked example: a search query
- Common mistakes
- Double-encoding
- Other frequent slips
- Frequently asked questions
- Should I use %20 or + for spaces?
- Is URL encoding a security measure?
- Does the case of the hex digits matter?
On this page
- What percent-encoding does
- Why reserved characters need escaping
- Encoding a whole URL vs encoding a single value
- Encoding a value (encodeURIComponent)
- Encoding a whole URL (encodeURI)
- A worked example: a search query
- Common mistakes
- Double-encoding
- Other frequent slips
- Frequently asked questions
- Should I use %20 or + for spaces?
- Is URL encoding a security measure?
- Does the case of the hex digits matter?
Every web developer eventually builds a link that breaks for no obvious reason. A search for "cats & dogs" returns results for just "cats". A file name with a space works on one server and 404s on another. A redirect URL passed as a parameter arrives with half of it missing. These are almost always the same bug: a character that means something to the URL's structure was used as ordinary data without being encoded.
URL encoding, formally called percent-encoding, is the fix. It's a small, precisely defined set of rules from RFC 3986, and once you understand what it protects against, knowing when and how to apply it becomes straightforward.
What percent-encoding does
Percent-encoding replaces a character with a % sign followed by two hexadecimal digits representing its byte value. A space is byte 0x20, so it becomes %20. An ampersand is 0x26, so it becomes %26. The receiving server decodes these sequences back into the original characters before your application sees them.
Characters outside plain ASCII are first converted to bytes using UTF-8, and each byte is encoded separately. The é in "café" is two bytes in UTF-8, so "café" becomes caf%C3%A9. Characters from Hindi, Chinese or emoji produce three or four encoded bytes each, which is why international URLs can look so long once encoded.
Some characters never need encoding. RFC 3986 calls them unreserved: the letters A–Z and a–z, the digits 0–9, and four symbols: hyphen, full stop, underscore and tilde. Anything outside that set is either reserved for a structural purpose or not allowed in a URL at all.
Why reserved characters need escaping
A URL isn't just a string — it's a structure, and certain characters mark where one part ends and the next begins. When one of those characters appears inside a value, the parser can't tell data from structure unless the data is encoded.
- ? marks the start of the query string.
- & separates one query parameter from the next.
- = separates a parameter's name from its value.
- # marks the start of the fragment, which browsers don't even send to the server.
- / separates path segments.
- + is decoded as a space by many form-handling libraries, so a literal plus sign must be sent as %2B.
Consider a search parameter whose value is cats & dogs. Written unencoded as ?q=cats & dogs, the & tells the server a new parameter is starting, so it sees q="cats " and a second, nameless parameter called " dogs". Nothing errors — you just silently get the wrong data. The same thing happens with #: everything after it is treated as a fragment and never reaches the server at all.
Encoding a whole URL vs encoding a single value
This is the distinction that causes the most confusion, and JavaScript's two built-in functions illustrate it neatly. They exist because the two jobs need opposite behaviour for the structural characters.
Encoding a value (encodeURIComponent)
When you're inserting a piece of data into a URL — a search term, a user name, a redirect target — you want every structural character in it escaped, because none of them should act as structure. encodeURIComponent escapes &, =, ?, #, /, + and everything else outside a small safe set. This is the right choice nearly every time you build a URL from parts.
Encoding a whole URL (encodeURI)
When you already have a complete URL and just need to make it safe — say, it contains spaces or non-ASCII characters — you must not escape the structural characters, or you'll destroy the structure itself. encodeURI leaves :, /, ?, &, = and # alone and only escapes characters that are never valid in a URL. The catch is that it can't fix a value that already contains an unescaped &, because it has no way to know that & was meant as data.
Tip
Encode values, not URLs. Build URLs by encoding each parameter value individually and joining the pieces yourself — or better, let a URL-building API do it. In JavaScript, new URL() with url.searchParams.set() handles the encoding for you and is much harder to get wrong than string concatenation.
A worked example: a search query
Suppose a user searches for cats & dogs = friends? and you need to send it as the q parameter to https://example.com/search.
- Raw value: cats & dogs = friends?
- Encoded as a component: cats%20%26%20dogs%20%3D%20friends%3F — spaces become %20, & becomes %26, = becomes %3D and ? becomes %3F.
- Final URL: https://example.com/search?q=cats%20%26%20dogs%20%3D%20friends%3F
- The server decodes q back to exactly cats & dogs = friends? — one parameter, nothing lost.
- Using form encoding instead (as URLSearchParams and HTML forms do), spaces become + rather than %20: q=cats+%26+dogs+%3D+friends%3F. Both are valid in a query string.
Compare what happens if the whole URL is run through encodeURI instead. The spaces get encoded, but the & and = inside the value are left as they are, because encodeURI assumes they're structure. The server then sees two broken parameters instead of one correct one.
Common mistakes
Double-encoding
Encoding is not idempotent: encoding a string twice doesn't leave it unchanged. The % sign is itself a reserved character, so encoding an already-encoded string turns every % into %25. A space that was correctly %20 becomes %2520, and after the server decodes it once, your application receives the literal text %20 instead of a space. Double-encoding usually happens when one layer of code encodes a value and a library or framework encodes it again automatically.
Watch out
If you see %25 in a URL where you didn't expect it — especially %2520, %253D or %2526 — something encoded the value twice. Find the second encoding step and remove it rather than decoding twice on the other end to compensate.
Other frequent slips
- Not encoding at all when concatenating user input into a URL, which is both a correctness bug and a common source of injection problems.
- Encoding a full URL with the component function, which escapes the :// and / characters and produces an unusable link.
- Assuming + always means space — it does in form-encoded query strings, but in a URL path a + is a literal plus sign.
- Forgetting that a redirect URL passed as a parameter is itself a value and needs encoding as a component, including its own ? and &.
Frequently asked questions
Should I use %20 or + for spaces?
In a URL path, always use %20. In a query string, both work with virtually every server, because form encoding (application/x-www-form-urlencoded) defines + as a space. %20 is the safer universal choice when you're unsure how the receiving side decodes.
Is URL encoding a security measure?
No. Anyone can decode it instantly, and it hides nothing. It keeps data from being misread as URL structure, which prevents some classes of bugs, but it isn't a substitute for validating input on the server or escaping output correctly for HTML.
Does the case of the hex digits matter?
No — %2F and %2f decode to the same character. RFC 3986 recommends uppercase for consistency, and most encoders produce it, but a correct decoder accepts either.
The rule underneath all of this is simple: structural characters are for structure, and anything that's data gets encoded. Encode each value once, as a component, at the moment you put it into the URL — and let the server decode it once on the way in.
Ready to put this into practice?
Encode a URL Free