URL encoding is one of those things you learn by getting burned. A search query breaks when someone types an ampersand, a signed API request fails because a + got mangled, or a redirect loses half its parameters. Nine times out of ten the fix is understanding what URL encoding does and where it applies.

What URL encoding actually is

URLs are only allowed to contain a limited set of ASCII characters: letters, digits, and a handful of punctuation marks. Everything else — spaces, accented letters, emoji, reserved characters used by the URL syntax itself — has to be represented some other way. That representation is percent-encoding: each disallowed byte becomes a % followed by its two-digit hexadecimal value.

A space becomes %20. An ampersand becomes %26. The letter é, when the string is UTF-8, becomes two bytes and encodes to %C3%A9. That last point matters: percent-encoding operates on bytes, not characters, so your encoding is only correct if everyone agrees the text is UTF-8 (which, in practice, it almost always is on the modern web).

Reserved vs unreserved characters

RFC 3986 splits characters into groups. The unreserved set — A-Z a-z 0-9 - _ . ~ — never needs encoding. The reserved set includes characters with structural meaning: / ? # [ ] @ ! $ & ' ( ) * + , ; =. These are the tricky ones, because whether they need encoding depends entirely on context.

A slash is fine when it separates path segments. A slash inside a single path segment — say, a filename that genuinely contains / — must be encoded as %2F, or the server will read it as two segments. Same character, different meaning depending on where it lives.

When you actually need it

You need URL encoding any time you're building a URL from data you don't fully control:

  • Query string values — the most common case. User input, search terms, filter values.
  • Path segments containing arbitrary strings, like usernames or document titles.
  • Form submissions with the application/x-www-form-urlencoded content type.
  • OAuth and API signatures, where the exact encoded form is part of what gets signed.

If you're using a proper HTTP client or a URL builder, a lot of this is handled for you. The bugs creep in when people concatenate strings by hand.

The gotchas that bite people

Spaces: %20 vs +

In the query string of an application/x-www-form-urlencoded form, a space is encoded as +. Everywhere else in a URL, a space is %20. These are not interchangeable in general. If you encode a path segment and your library uses + for spaces, a server that doesn't treat + specially in paths will hand you a literal plus sign. This mismatch is behind a surprising number of "it works locally but not in production" tickets.

Double encoding

Encoding something that's already encoded turns % into %25, so %20 becomes %2520. This happens when a value passes through two layers that both "helpfully" encode. The symptom is literal percent sequences showing up in your UI. The fix is to be deliberate about which layer owns the encoding and decode exactly once on the way back.

Picking the wrong function

In JavaScript, encodeURI and encodeURIComponent are different tools. encodeURI assumes you're handing it a full URL and leaves reserved characters like / ? & = alone. encodeURIComponent encodes a single component and will escape those characters. Use encodeURIComponent for individual query values; use encodeURI only on a whole URL you trust the structure of. Reaching for the wrong one is a classic mistake.

Encoding the whole URL

Never run an entire URL through a component encoder. You'll destroy the ://, the slashes, and the ?, leaving a useless blob. Encode the parts, then assemble them.

A short worked example

Say you're building a search URL for the term fish & chips (large). Naively concatenating it produces a broken URL — the & starts a new parameter and the parentheses and spaces are questionable.

const base = 'https://example.com/search';
const q = 'fish & chips (large)';

// Wrong: the & breaks the query, spaces are invalid
const bad = `${base}?q=${q}`;
// https://example.com/search?q=fish & chips (large)

// Right: encode the value
const url = `${base}?q=${encodeURIComponent(q)}`;
// https://example.com/search?q=fish%20%26%20chips%20(large)

console.log(url);

Notice the space became %20 and the ampersand became %26, so the query is now a single intact value. On the server, decoding gives you back the exact original string. For building many parameters at once, let the platform do the work:

const params = new URLSearchParams({
  q: 'fish & chips (large)',
  page: '2'
});
const url = `https://example.com/search?${params}`;
// https://example.com/search?q=fish+%26+chips+%28large%29&page=2

Here URLSearchParams follows the form-encoded rules — note the + for spaces and the encoded parentheses. Both approaches are correct; just know which rules each one uses so you decode with the matching logic.

Debugging encoded strings

When a URL looks wrong, the fastest way to confirm what's happening is to decode it and see what you actually have. I keep a URL Encoder / Decoder open for exactly this — paste in the mystery string, decode it, and the double-encoding or wrong-space-character jumps out immediately. It beats squinting at %2520 in a browser address bar.

Rules of thumb

  • Encode values, not whole URLs.
  • Pick the encoder that matches the context: component vs full URL, path vs form.
  • Assume UTF-8 and make sure both ends agree.
  • Decode exactly once. If you see %25 where you expected %, you've double-encoded somewhere upstream.
  • When in doubt, decode the string and look at the bytes instead of guessing.

Get these straight and an entire category of flaky, hard-to-reproduce bugs disappears. URL encoding isn't complicated — it just rewards paying attention to context.

Photo by Chris Ried on Unsplash