If you've ever seen %20 in a URL and wondered why a space got turned into that, you've already met URL encoding. It's one of those quiet mechanisms that works fine until it doesn't — and when it breaks, you usually find out via a mangled query string or a broken redirect that only fails for one customer with a + in their name.

Here's what URL encoding actually does, when you need it, and the handful of mistakes that keep showing up in code reviews.

What URL encoding is

URLs are only allowed to contain a limited set of characters: letters, digits, and a small group of punctuation marks like -, _, ., and ~. Everything else — spaces, accented letters, emoji, slashes that aren't path separators — has to be represented in a safe way.

That's what percent-encoding (the formal name) does. Each disallowed byte is written as a % followed by its two-digit hexadecimal value. A space is byte 0x20, so it becomes %20. The ampersand & is 0x26, so it becomes %26. Non-ASCII characters get encoded as their UTF-8 bytes first, then each byte is percent-encoded — which is why é turns into %C3%A9, two bytes, not one.

When you actually need it

You need encoding any time user-controlled or arbitrary data goes into a URL. The classic cases:

  • Query parameters. Building ?q=... with a search term that might contain &, =, or spaces.
  • Path segments. Putting a filename or slug into the path, e.g. /files/my%20report.pdf.
  • Redirect targets. Passing a return URL as a parameter, like ?next=%2Fdashboard%3Ftab%3Dbilling.
  • API calls. Appending values to REST endpoints where a stray character would change the structure of the request.

The rule of thumb: if you're concatenating a dynamic value into a URL string by hand, something probably needs encoding.

The gotchas that bite

1. Encoding the whole URL instead of the parts

This is the big one. People reach for a single encode function and run the entire URL through it, which destroys the :, /, and ? that give the URL its structure. Encoding is per-component. You encode the value of a query parameter, not the = or & that separate parameters.

2. encodeURI vs encodeURIComponent in JavaScript

JavaScript gives you two functions and they are not interchangeable. encodeURI assumes you're handing it a complete URL and leaves reserved characters like &, =, ?, and / alone. encodeURIComponent assumes you're handing it a single component and encodes those reserved characters too. For query values, you almost always want encodeURIComponent.

const term = "fish & chips";

encodeURI(term);          // "fish%20&%20chips"  (& left intact — wrong for a value)
encodeURIComponent(term); // "fish%20%26%20chips"   (& encoded — correct)

3. The + vs %20 trap

In the path and in query strings, a space encodes as %20. But in the older application/x-www-form-urlencoded format used by HTML form submissions, a space is encoded as +. That means a literal + in form data has to become %2B. Mix these up and "C++" becomes "C " after a round trip. Know which format you're working with.

4. Double-encoding

Encode a string that's already encoded and %20 becomes %2520 — because the % itself gets encoded to %25. This happens a lot when a value passes through several layers (frontend, a proxy, a backend) and each one helpfully encodes it again. If you see %25 showing up where you didn't expect it, suspect a double-encode somewhere in the chain.

5. Assuming decoding is always safe

Decoding untrusted input can turn an innocuous string into path separators or protocol prefixes. Always decode in a context where you validate the result, not blindly.

A worked example

Say you're building a link to a search page and the user typed price < 50 & in stock. You want that as the value of a q parameter.

const base = "https://shop.example.com/search";
const query = "price < 50 & in stock";

const url = `${base}?q=${encodeURIComponent(query)}`;
// https://shop.example.com/search?q=price%20%3C%2050%20%26%20in%20stock

Notice that < became %3C, & became %26, and the spaces became %20. If you'd skipped encoding, the & would have been read as a parameter separator and your server would have seen two broken parameters instead of one.

A cleaner approach for anything non-trivial is to let the platform build the query for you, so you don't forget a component:

const url = new URL("https://shop.example.com/search");
url.searchParams.set("q", "price < 50 & in stock");
url.searchParams.set("page", "2");

url.toString();
// https://shop.example.com/search?q=price+%3C+50+%26+in+stock&page=2

Worth noting: URLSearchParams uses the form-encoding convention, so spaces come out as + here rather than %20. Both decode back to a space on a compliant server, but it's another reminder that the two conventions coexist.

Decoding and quick checks

Decoding reverses the process — decodeURIComponent("price%20%3C%2050") gives you back price < 50. The common failure is calling it on a string that wasn't fully encoded, which throws a URIError on malformed sequences like a lone %.

When I'm debugging a weird URL — a redirect that won't fire, a parameter arriving garbled — I don't trust my mental hex table. I paste the string into a URL Encoder / Decoder to see the clean before-and-after instantly. It's the fastest way to spot a double-encode or confirm whether that %2B really is a plus sign.

The short version

  • Encode values, not whole URLs.
  • In JavaScript, reach for encodeURIComponent for parameter values.
  • Watch the +-vs-%20 difference between form encoding and the rest.
  • If you see %25 where you didn't put it, you've double-encoded.
  • Prefer URL / URLSearchParams to hand-built strings when you can.

Get those right and URL encoding stops being a source of mystery bugs and goes back to being the boring, reliable plumbing it's meant to be.

Photo by Pankaj Patel on Unsplash