URL encoding explained: percent-encoding, %20 vs +, and encoding a value vs a whole URL
Which characters must be escaped in a URL, why spaces become %20 in one place and + in another, and the double-encoding bugs that break links.
Every web developer eventually ships a link that breaks because a search term contained an ampersand, or sees %2520 in a log and wonders where it came from. URL encoding, also called percent-encoding, is simple in principle, but there are two different encoders and an old form convention that make it easy to get wrong. This guide explains the rules and the mistakes behind most bugs.
Why URLs need encoding at all
A URL is a structured string. Some characters have a job inside it: / separates path segments, ? starts the query, & separates parameters, = splits a name from its value, and # starts the fragment. If your data contains one of those characters, the parser cannot tell your data apart from the structure.
Imagine a search link built by gluing strings together: /search?q= plus salt & pepper. The server sees a parameter q with the value salt and a second, empty parameter called pepper. Encoding the value first gives /search?q=salt%20%26%20pepper, which arrives intact.
How percent-encoding works
The rule from RFC 3986 is that a character which is not allowed literally is replaced by % followed by two hexadecimal digits for each of its bytes. A space is byte 0x20, so it becomes %20. An ampersand is 0x26, so %26.
Characters outside ASCII are first converted to UTF-8 bytes, and then each byte is percent-encoded. That is why é (two bytes in UTF-8) becomes %C3%A9, and the Chinese character 中 (three bytes) becomes %E4%B8%AD. When you see long runs of %XX sequences in a URL, it is usually ordinary text in a non-Latin script.
A small set of characters never needs encoding — the "unreserved" characters: letters, digits, and -, ., _, ~. Everything else either has a structural meaning or has to be escaped.
Encoding a value vs encoding a whole URL
JavaScript has two functions here, and they are not interchangeable:
encodeURIComponent()is for a single piece of a URL — one query value, one path segment. It escapes everything except the unreserved characters, including/ ? & = #.encodeURIComponent('a b&c=d/é')givesa%20b%26c%3Dd%2F%C3%A9.encodeURI()is for a complete URL that is already correctly structured but might contain spaces or non-ASCII characters. It leaves the structural characters alone so the URL keeps working:encodeURI('https://x.com/a b?q=1&r=é')giveshttps://x.com/a%20b?q=1&r=%C3%A9.
The common bug is using encodeURI on a value. It will not escape an & or = inside the value, so the query string breaks exactly as in the salt-and-pepper example. The opposite mistake — running encodeURIComponent over a whole URL — turns https:// into https%3A%2F%2F and produces something that is no longer a link.
The rule of thumb: encode each value separately, then assemble the URL. Better still, let the platform do it — new URL() and URLSearchParams in JavaScript, or the equivalent in your server framework, apply the right encoding to each part automatically.
Why a space is sometimes %20 and sometimes +
This one confuses almost everyone, and the explanation is historical. The URL specification encodes a space as %20. But HTML forms submitted with the application/x-www-form-urlencoded content type use an older convention in which a space is written as +, and a literal plus sign must be written as %2B.
You can see both in one browser: encodeURIComponent('a b') returns a%20b, while new URLSearchParams({ q: 'a b+c' }).toString() returns q=a+b%2Bc.
In the query string both forms are normally decoded to a space by servers, so either works there. The danger is in the other direction: if a value containing a real + — a phone number like +44… or a Base64 string — is put into a query without encoding, a form-style decoder turns the plus into a space and silently corrupts it. In the path of a URL, + is just a plus sign and only %20 means a space.
Double encoding
If a value is encoded twice, the % sign from the first pass is itself encoded to %25 on the second. A space becomes %20 and then %2520. When the server decodes once, it gets the literal text %20 instead of a space.
Double encoding usually happens when two layers each "helpfully" encode: a frontend calls encodeURIComponent and then passes the result to an HTTP library that encodes query parameters again. If you see %25 followed by two hex digits in a log, look for two encoders in the chain and remove one.
Decoding untrusted URLs
Decoding is how you find out what a suspicious link really points to. Phishing links often percent-encode the real destination, or hide it in a redirect parameter, so that it is not readable at a glance. Pasting the link into a decoder shows the plain text without ever opening it.
Be aware of decodeURIComponent's one sharp edge: a stray % that is not followed by two hex digits — 100% on its own, for instance — makes it throw a URIError. Code that decodes user input should catch that error rather than crash.
The URL Encoder on this site lets you switch between component and full-URL modes and decodes in your browser, so the link you are inspecting is never opened or sent anywhere.
Checklist
- Encode each query value and path segment individually with a component encoder.
- Prefer
URL/URLSearchParamsor your framework's builder over string concatenation. - Encode a literal
+as%2Bin query strings. - If you see
%25XX, something encoded twice. - Don't use Base64 to make text URL-safe; percent-encoding keeps it readable. If you must put Base64 in a URL, use the URL-safe alphabet — see Base64 explained.