Percent-encoding is one of those things you half-understand until a bug forces you to learn it properly. Usually it's a user with a + in their search query, or a redirect URL that keeps breaking when it contains another URL. The rules aren't complicated, but they're easy to get subtly wrong.

Here's the practical version — what URL encoding is, where it bites, and how to reason about it.

What URL encoding actually is

A URL can only contain a limited set of characters. The spec (RFC 3986) splits characters into reserved ones that have special meaning (/, ?, #, &, =, : and friends) and unreserved ones that are always safe (letters, digits, -, ., _, ~).

Anything outside the safe set — spaces, accented letters, emoji, or a reserved character used as literal data — has to be percent-encoded. That means taking the byte value and writing it as a % followed by two hex digits. A space becomes %20. An ampersand becomes %26.

So this:

https://example.com/search?q=cats & dogs

should really be:

https://example.com/search?q=cats%20%26%20dogs

Without the encoding, that bare & looks like a parameter separator, and your server reads two parameters instead of one search string.

When you actually need it

You need encoding whenever you put arbitrary data into a URL. The common cases:

  • Query string values — search terms, filter values, anything a user typed.
  • Path segments containing user data, like /users/john%20smith.
  • Embedding a URL inside a URL — the classic OAuth redirect_uri or any callback parameter.
  • Non-ASCII text — names, labels, and anything that isn't plain Latin letters gets UTF-8 encoded first, then percent-encoded.

If the data is hardcoded and you know it's clean, you can skip it. The moment anything comes from a user, a database, or another service, encode it.

The gotchas that cause real bugs

Space: %20 vs +

In the query string of a URL, spaces are sometimes encoded as + instead of %20. This comes from the old application/x-www-form-urlencoded format used by HTML forms. Both show up in the wild, and they are not interchangeable everywhere.

In the path portion of a URL, + means a literal plus sign, not a space. Decode /files/a+b expecting a space and you'll get the wrong filename. Know which context you're in.

Double encoding

This is the one that wastes an afternoon. You encode a string, store it, then something encodes it again. Now %20 becomes %2520 because the % itself got encoded.

space   -> %20   (correct)
%20     -> %2520 (double-encoded, wrong)

If you see %25 showing up unexpectedly in logs, you're almost certainly encoding something that was already encoded. Encode exactly once, at the boundary where you build the URL.

Encoding the whole URL instead of the parts

Don't run a full URL through an encoder — you'll mangle the slashes and colons that are supposed to stay as structure. Encode each value separately before you assemble the URL.

Choosing the wrong JavaScript function

JavaScript gives you two functions and they do different jobs:

  • encodeURI() — for encoding a complete URL. It leaves reserved characters like /, ?, & alone.
  • encodeURIComponent() — for encoding a single value. It encodes those reserved characters too.

For a query parameter value, you almost always want encodeURIComponent(). Using the wrong one is a frequent source of broken parameters.

A short worked example

Say you're building a link that passes a redirect URL and a user's display name as query parameters:

const base = "https://app.example.com/login";
const redirect = "https://app.example.com/dashboard?tab=settings";
const name = "Jörg & Co.";

const url = base
  + "?redirect=" + encodeURIComponent(redirect)
  + "&name=" + encodeURIComponent(name);

console.log(url);

That produces:

https://app.example.com/login?redirect=https%3A%2F%2Fapp.example.com%2Fdashboard%3Ftab%3Dsettings&name=J%C3%B6rg%20%26%20Co.

Notice what happened. The redirect URL's own ?, =, and / characters all became percent-encoded, so they don't interfere with the outer URL's structure. The ö became %C3%B6 — that's its two UTF-8 bytes. The & in the name became %26, so it isn't mistaken for a parameter separator.

On the receiving end, the server decodes each parameter and gets the original values back, intact. That round trip only works because you encoded the values and nothing else.

Decoding, and sanity-checking by hand

Decoding reverses the process: every %XX becomes the byte it represents, and groups of bytes get interpreted as UTF-8 text. In JavaScript, decodeURIComponent() handles it.

When something looks off, it helps to paste the string into a tool and watch it decode step by step. I keep URL Encoder / Decoder open when I'm debugging callback URLs — it makes double-encoding obvious instantly, because you can decode once, see the %25 turn back into %, and realise you need another pass.

A few rules to keep you out of trouble

  • Encode values at the point you build the URL, not earlier and not later.
  • Encode each component separately — never the whole URL at once.
  • Use encodeURIComponent() for query values; reach for encodeURI() only when handling a full URL.
  • Be deliberate about + versus %20 depending on whether you're in a path or a form-encoded query.
  • Match your encode and decode steps one-to-one. One encode, one decode.

Percent-encoding isn't glamorous, but it's the kind of fundamental that silently causes flaky behaviour when it's wrong. Get the mental model right — encode the parts, once, at the boundary — and the mysterious parameter bugs mostly disappear.

Photo by Florian Olivo on Unsplash