URL encoding is one of those things you half-understand until a bug forces you to really learn it. A query parameter with a plus sign that mysteriously becomes a space. A signature that fails validation because someone encoded a slash that shouldn't have been touched. An API returning 400 because a # in the path got treated as a fragment. These are all the same problem wearing different hats.

Here's what's actually going on, and how to stop getting burned by it.

What URL encoding actually is

URLs can only safely contain a limited set of characters. The spec (RFC 3986) splits characters into unreserved (letters, digits, and - _ . ~) and reserved (things like / ? # & = : @) that have special meaning in a URL. Anything outside the safe set — spaces, accented letters, emoji, or a reserved character you want to use literally rather than structurally — has to be percent-encoded.

Percent-encoding means taking the byte value of a character and writing it as % followed by two hex digits. A space becomes %20. An ampersand becomes %26. A non-ASCII character is first encoded as UTF-8 bytes, then each byte is percent-encoded, so é becomes %C3%A9.

When you need it

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

  • Query string values — search terms, filters, redirect URLs passed as parameters.
  • Path segments — slugs or IDs that might contain spaces or special characters.
  • Form submissions — application/x-www-form-urlencoded bodies follow nearly the same rules.
  • OAuth and signed requests — signatures are computed over encoded values, so getting the encoding wrong breaks auth entirely.

The classic example is putting a full URL inside another URL:

https://example.com/go?next=https://app.example.com/dashboard?tab=billing

This is broken. The second ? and the & that would follow get interpreted as part of the outer URL's query string. The next parameter needs to be encoded as a single opaque value:

https://example.com/go?next=https%3A%2F%2Fapp.example.com%2Fdashboard%3Ftab%3Dbilling

The gotchas that bite people

encodeURI vs encodeURIComponent

JavaScript gives you two functions and they are not interchangeable. encodeURI is for encoding a whole URL — it leaves reserved characters like / ? & = alone because they have structural meaning. encodeURIComponent is for encoding a single piece that goes inside a URL, so it encodes those reserved characters too.

encodeURI("https://x.com/a b?c=d&e=f");
// "https://x.com/a%20b?c=d&e=f"  (structure preserved)

encodeURIComponent("https://x.com/a b?c=d&e=f");
// "https%3A%2F%2Fx.com%2Fa%20b%3Fc%3Dd%26e%3Df"  (fully encoded)

Rule of thumb: use encodeURIComponent for each individual parameter name and value, and let your URL-building code handle the structure. If you reach for encodeURI, you probably want something else.

Spaces: %20 vs +

In the path and in modern query strings, a space is %20. But in the older application/x-www-form-urlencoded format, a space is encoded as +. This mismatch is a constant source of confusion. If you decode a form body with a generic URL decoder, a literal + won't be turned into a space — and a real space that was sent as %20 will decode fine but a + stays a +. Know which context you're in.

Double encoding

Encode once. Encoding an already-encoded string turns %20 into %2520 (because the % itself gets encoded). This usually happens when one layer of your stack encodes a value and another layer does it again. Symptoms include %2520 showing up in logs or URLs that look like they have too many percent signs.

Trusting the browser's address bar

Browsers display decoded URLs for readability, so what you see isn't always what's sent over the wire. When debugging, look at the actual request in your network tab or server logs, not the pretty version in the address bar.

Characters that don't need encoding but often get it

The unreserved set — including -, _, ., and ~ — never needs encoding. Some older libraries encode ~ anyway, which has caused signature mismatches in APIs like AWS's older signing schemes. Stick to standards-compliant tooling.

A short worked example

Say you're building a link to a search results page and the user searched for coffee & tea (50% off). Here's the right way to construct it in JavaScript:

const query = "coffee & tea (50% off)";
const url = `https://shop.example.com/search?q=${encodeURIComponent(query)}`;

console.log(url);
// https://shop.example.com/search?q=coffee%20%26%20tea%20(50%25%20off)

Notice the & became %26 and the % became %25. If you'd skipped encoding, the & would have started a second, bogus query parameter and everything after it would be lost.

Even better, use the URL and URLSearchParams APIs, which handle encoding for you:

const u = new URL("https://shop.example.com/search");
u.searchParams.set("q", "coffee & tea (50% off)");
console.log(u.toString());
// https://shop.example.com/search?q=coffee+%26+tea+%2850%25+off%29

Note that URLSearchParams uses the form-encoding convention, so spaces become +. Both forms are valid query strings; just be aware of the difference when you're comparing strings or writing tests.

When you just need to check something fast

Half the time you're not writing code — you're staring at a mangled URL in a log file trying to figure out what the original value was. Rather than firing up a REPL, paste it into a quick URL Encoder / Decoder and read the decoded result instantly. It's handy for spotting double encoding too: if decoding once still leaves % sequences, you know something encoded it twice.

The short version

  • Encode any arbitrary data before putting it in a URL.
  • Use encodeURIComponent for individual parts, not encodeURI, in most cases.
  • Watch for + vs %20 depending on query-string vs form encoding.
  • Never encode twice.
  • Prefer URL/URLSearchParams so you're not hand-rolling encoding logic.

Get these right and a whole category of flaky, hard-to-reproduce bugs just disappears.

Photo by Chris Ried on Unsplash