URL encoding is one of those topics you think you understand until a customer's name with a space in it breaks your redirect. Or an ampersand in a query string silently truncates half your parameters. It's a small thing that causes a surprising number of production bugs, usually because people assume the browser or their HTTP library "just handles it."

Let's clear up what it actually does and where it matters.

What URL encoding actually is

A URL can only reliably contain a limited set of ASCII characters. Letters, digits, and a handful of punctuation marks are safe. Everything else — spaces, slashes used as data rather than path separators, accented characters, ampersands, emoji — has to be represented using percent-encoding.

Percent-encoding replaces an unsafe character with a % followed by its byte value in hexadecimal. A space becomes %20. An ampersand becomes %26. Non-ASCII characters are first encoded as UTF-8 bytes, then each byte is percent-encoded. So é (two bytes in UTF-8) becomes %C3%A9.

That's the whole mechanism. The complexity comes from knowing which characters need encoding and where.

Reserved vs. unreserved characters

RFC 3986 splits characters into groups. The ones that matter day to day:

  • Unreserved — A-Z a-z 0-9 - _ . ~. These never need encoding.
  • Reserved — characters like / ? # [ ] @ ! $ & ' ( ) * + , ; =. These have special meaning in a URL. You only encode them when you want their literal value as data rather than as structure.

This last point is the crux of most bugs. A & that separates query parameters must stay raw. A & that's part of a company name in a parameter value must be encoded as %26. Same character, different intent, and the encoder can't guess your intent for you.

When you actually need it

You need URL encoding whenever you're building a URL from values you don't fully control:

  • Putting user input into a query string
  • Constructing redirect URLs that carry another URL as a parameter
  • Building API requests with search terms, filters, or free-text fields
  • Passing data through Location headers or OAuth callback URLs

The redirect case deserves special attention. Nesting a URL inside another URL is where people get burned constantly, because the inner URL contains its own ?, &, and = that will be misinterpreted if not encoded.

Query strings vs. form bodies: the space problem

Here's a classic gotcha. In a standard URL component, a space is %20. But in application/x-www-form-urlencoded data — the format used by HTML form submissions and often query strings — a space is traditionally encoded as +.

This is why JavaScript gives you two different functions:

encodeURIComponent("a b&c")  // "a%20b%26c"
// vs. form-style encoding used by URLSearchParams:
new URLSearchParams({ q: "a b&c" }).toString()  // "q=a+b%26c"

Both are valid. A compliant decoder handles both + and %20 as spaces in form data. But if you decode with the wrong assumption — treating a + literally when it meant a space, or vice versa — you get corrupted data. Know which context you're in.

The encodeURI vs encodeURIComponent trap

JavaScript's two built-in functions do different jobs and mixing them up is a frequent mistake:

  • encodeURI() encodes a whole URL and leaves structural characters like /, ?, and & untouched.
  • encodeURIComponent() encodes a single component (one parameter value) and escapes those structural characters too.

Rule of thumb: if you're building one piece of a URL, use encodeURIComponent. If you ever find yourself running encodeURI on a parameter value, stop — it won't escape the & and your URL will break.

A short worked example

Say you're building a search link and the user searched for tom & jerry 50% off. Naively concatenating gives you a broken URL:

// BROKEN - the & splits the params, % is invalid
https://shop.example.com/search?q=tom & jerry 50% off&sort=price

The & in the search term gets read as a parameter separator, so the server sees q=tom and a mysterious jerry 50% off parameter. The bare % is also malformed. Encode the value properly:

const q = encodeURIComponent("tom & jerry 50% off");
// "tom%20%26%20jerry%2050%25%20off"
const url = `https://shop.example.com/search?q=${q}&sort=price`;
// https://shop.example.com/search?q=tom%20%26%20jerry%2050%25%20off&sort=price

Now the server decodes q back to the exact original string, and sort=price stays separate.

Common gotchas worth remembering

  • Double encoding. Encoding an already-encoded string turns %20 into %2520. This happens when a value passes through two layers that both encode. Encode once, as close to the final URL construction as possible.
  • The plus sign. A literal + in a value must be encoded as %2B, otherwise a form-style decoder reads it as a space. This burns people with phone numbers and base64 strings.
  • Encoding the whole URL. Don't run a component encoder over a complete URL — it'll mangle the slashes and colons.
  • Non-ASCII in paths. Modern browsers display Unicode URLs nicely but transmit them percent-encoded. Don't assume the raw characters reach your server untouched.

When you just need to check something fast

Half the time you don't want to write code — you want to paste a weird-looking string and see what it decodes to, or encode a value for a quick test. For that I keep a bookmark to Devsly's URL Encoder / Decoder. Drop in the string, see the result instantly, confirm your assumption, move on. It's handy for debugging a malformed redirect or verifying that %2520 you spotted really is double-encoded.

The takeaway

URL encoding isn't complicated once you internalize one idea: encode values, not structure. Use encodeURIComponent for the parts, let the structural characters stay raw, encode exactly once, and watch out for the +-versus-%20 distinction in form data. Get those right and most of your mysterious parameter-truncation bugs will disappear.

Photo by Florian Olivo on Unsplash