URL encoding explained

URLs can officially contain only a small set of characters — letters, digits and a handful of punctuation marks. Everything else travels as %XX escape sequences. Most URL bugs come from choosing the wrong level of encoding, not from encoding itself.

Why percent-encoding exists

A URL is a structured address, and its punctuation is load-bearing: ? starts the query, & separates parameters, = links keys to values, / splits paths, # starts the fragment. If your data contains any of these characters, the URL's structure would change — so the character gets replaced by a percent sign followed by its byte value in hexadecimal: a space becomes %20, ? becomes %3F, and the Chinese character 中 (UTF-8 bytes E4 B8 AD) becomes %E4%B8%AD.

That last example shows the two layers: first the text is turned into bytes (UTF-8), then unsafe bytes are percent-escaped. Non-ASCII characters can never appear literally in a strictly encoded URL.

The two levels of encoding — and the classic bug

JavaScript ships two functions that look interchangeable and are not:

FunctionIntended forLeaves aloneEncodes
encodeURIA complete URL:// ? & = / # — all structureSpaces, non-ASCII, and most punctuation in the data
encodeURIComponentA single value inside a URLA-Z a-z 0-9 - _ . ! ~ * ' ( )Everything structural, including &, =, ?

The bug writes itself: encode a full URL with encodeURIComponent and its :// becomes %3A%2F%2F, breaking it; encode a query parameter with encodeURI and an & inside the value splits the parameter in two. The rule: encodeURI for whole URLs, encodeURIComponent for each piece you insert into one.

Worked example

Building a search link for the query Tom & Jerry 2026?:

  • Wrong: encodeURI('/search?q=' + q) → /search?q=Tom & Jerry 2026? — the raw & makes the server see two parameters: q=Tom and Jerry 2026?.
  • Right: '/search?q=' + encodeURIComponent(q) → /search?q=Tom%20%26%20Jerry%202026%3F — one parameter, value intact.

The plus sign: two standards, one headache

In application/x-www-form-urlencoded — the format of HTML form submissions and many query strings — a space is written as +. In RFC 3986 percent-encoding, a space is %20 and a literal + is %2B. Servers differ on whether they decode + as a space or a plus. If a user's input legitimately contains + (phone numbers are the classic case: +86), always encode it as %2B or the server may read a space. This single ambiguity explains a decade of "why does my phone number lose its plus" bug reports.

Double-encoding: the invisible bug

Encoding an already-encoded string encodes the percent signs: %20 becomes %2520. Decode it once and you get %20 — still not a space. Symptoms: parameters that display fine in logs but arrive mangled at the server, or the infamous %2520 appearing in analytics. The rule: encode once, at the boundary where the value enters the URL, and never re-encode a complete URL. If you must debug one, decode in the URL tool repeatedly until the output stops changing — the number of decodes tells you how many times it was encoded.

Reserved vs unreserved characters

  • Reserved (must be encoded in data): : / ? # [ ] @ ! $ & ' ( ) * + , ; = — each has structural meaning somewhere in a URL.
  • Unreserved (never need encoding): A-Z a-z 0-9 - _ . ~ — encoding them is legal but only adds bytes.

When in doubt about what a string will look like at the other end, paste it into the URL encoder, encode and decode it, and compare — the tool also splits a URL into scheme, host, path and query parameters so you can see exactly which parts are structure and which are data.