harmmy

URL encoding: spaces, slashes and the %25 trap

encodeURI and encodeURIComponent solve different problems. Where + and %20 diverge, how double encoding produces %2520, and which characters need escaping.

2026-12-02

Percent-encoding works on bytes, not characters

A URL may only contain a small ASCII set. Everything else is written as a percent sign and two hex digits per byte, so a space is %20 and a euro sign is %E2%82%AC because UTF-8 encodes it in three bytes. The hex is case-insensitive, but uppercase is the convention and some signature checks compare the exact string.

Characters in A–Z, a–z, 0–9 and - . _ ~ never need encoding and should stay literal. Reserved characters such as : / ? # [ ] @ ! $ & ' ( ) * + , ; = have meaning in the URL grammar; they may be escaped only when they are data rather than syntax. Encoding ~ as %7E is legal, but some canonicalisers rewrite it, which matters for signed requests.

encodeURI leaves structure alone, encodeURIComponent does not

encodeURI is for a whole URL: it encodes spaces, quotes and non-ASCII but leaves : / ? # & = intact, because those are the structure. encodeURIComponent is for one value: it encodes / ? & = : as well, so the value cannot break out of its slot. Using the wrong one is the most common cause of a broken query string.

Concretely, encodeURIComponent("a/b?c=d") gives a%2Fb%3Fc%3Dd, while encodeURI keeps a/b?c=d. If you are building a query with a value appended after q=, the value must go through encodeURIComponent. If you encode a full URL with encodeURIComponent before putting it into a redirect parameter, the receiving side must decode it once to get a usable URL back.

Space is + in forms and %20 in paths

HTML form submission uses application/x-www-form-urlencoded, where a space becomes + in the query string. RFC 3986 says a space is %20, and in a path + is a literal plus, not a space. URLSearchParams follows the form convention and serialises a space as +, which surprises people who expect %20.

Servers are inconsistent: many decode + as space in the query but treat it literally in the path, and a few decode it everywhere. If a value may contain a real plus, such as a phone number +49 or a base64 fragment, encode the plus as %2B or the server will turn it into a space. Test both a space and a plus before trusting a client library.

Double encoding and illegal percent signs

Encoding twice turns %20 into %2520, because the percent of %20 is itself encoded as %25. This happens when a value is encoded once by the application and again by an HTTP library, or when a signature is computed on the raw value but sent encoded. The symptom is a literal %20 appearing in the page, a search that finds nothing, or a signature mismatch.

A percent sign not followed by two hex digits, such as %zz, %2 or a trailing %, is invalid. Decoders differ: some throw, some pass it through unchanged, and some replace it with U+FFFD. That is why a URL that works in one stack fails in another. When validating, reject malformed sequences instead of guessing, and never decode repeatedly in a loop.

Common bugs and how to check the round trip

Encoding a path segment with encodeURIComponent turns / into %2F, and many routers then refuse the request or decode it back into a separator. Encode each segment with encodeURIComponent and join with literal /, or use encodeURI on a path you know is already structured. For query parameters, build with URL and URLSearchParams rather than string concatenation.

The reliable check is a round trip: take the original value, encode it, decode it once with the same convention the server uses, and compare. Then look at the server log to see the string that actually arrived, because proxies and frameworks sometimes normalise before your handler runs. If a signature is involved, compare the canonical string byte for byte; a difference in %20 versus + or in the order of parameters is enough to fail.

Related tools

URL encoding: spaces, slashes and the %25 trap | harmmy