URL encoding is one of those things that works fine until it suddenly doesn't. You paste a link into a chat app, it breaks. You build a query string by hand, and a stray & swallows half your parameters. Someone's name has a space in it and your API throws a 400. Almost every one of these problems comes down to encoding — or the lack of it.

What URL encoding actually is

URLs are only allowed to contain a limited set of ASCII characters. The spec (RFC 3986) splits characters into a few groups: unreserved characters you can use freely (letters, digits, -, ., _, ~), reserved characters that have special meaning (/, ?, #, &, =, :, and friends), and everything else.

Percent-encoding — the formal name — replaces anything outside the safe set with a % followed by the two-digit hex value of the byte. A space becomes %20. An ampersand becomes %26. A non-ASCII character like é gets UTF-8 encoded first, then each byte is percent-encoded, so it ends up as %C3%A9.

That last detail matters: encoding operates on bytes, not characters. Always encode UTF-8 text, and you'll avoid a whole category of mojibake bugs.

When you actually need it

The short version: whenever you put arbitrary data into a URL. More specifically:

  • Query string values — search terms, emails, filters, anything a user typed.
  • Path segments that contain slashes, spaces, or non-ASCII characters.
  • OAuth and signing — signature schemes are notoriously strict about exactly which characters get encoded.
  • Redirect parameters — when one URL carries another URL as a value, the inner one must be encoded or its ? and & will leak into the outer one.

That redirect case is the classic trap. Consider this:

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

The ?tab=billing on the inner URL gets parsed as a parameter of the outer URL, not part of next. You need to encode the whole inner value first.

The gotchas that bite everyone

1. Encoding the whole URL instead of the parts

This is the big one. Do not run an entire URL through an encoder — you'll turn https:// into https%3A%2F%2F and break it. Encode values, not structure. Build the URL skeleton yourself and encode each piece as you insert it.

2. encodeURI vs encodeURIComponent

JavaScript gives you both, and they are not interchangeable. encodeURI assumes you're passing a full URL and leaves reserved characters like &, =, and / alone. encodeURIComponent encodes them — which is exactly what you want for individual query values.

const value = "a&b=c";
encodeURI(value);          // "a&b=c"   (wrong for a param)
encodeURIComponent(value); // "a%26b%3Dc" (correct)

3. Spaces: %20 vs +

In the path portion of a URL, a space is %20. In the query string, both %20 and + have historically meant space because of the older application/x-www-form-urlencoded format. Most servers accept either, but if you're decoding manually, remember that + might mean space in a query — and might mean a literal plus in a path. Mixing them up corrupts data silently.

4. Double encoding

Encode something twice and %20 becomes %2520 (because the % itself gets encoded to %25). This usually happens when a value passes through two layers that both "helpfully" encode. The symptom is literal %20 text showing up in your UI. Encode exactly once, at the boundary where you build the URL.

5. Trusting the browser address bar

Browsers display decoded URLs for readability, so the bar can lie about what's actually being sent. Check the network tab or your server logs when debugging, not the pretty version.

A short worked example

Say you're building a search link where a user searched for café & bar and you want to pass their email as well.

const params = new URLSearchParams({
  q: "café & bar",
  email: "[email protected]",
});

const url = `https://example.com/search?${params}`;
// https://example.com/search?q=caf%C3%A9+%26+bar&email=jo%2Btest%40example.com

Using URLSearchParams is the right move in modern JavaScript — it handles encoding for you and keeps you from building query strings with string concatenation, which is where the bugs live. Notice the + in the email became %2B (a literal plus), the ampersand became %26, and é became %C3%A9. All correct.

On the Python side, the equivalent is urllib.parse:

from urllib.parse import urlencode

params = {"q": "café & bar", "email": "[email protected]"}
query = urlencode(params)
url = f"https://example.com/search?{query}"

When you just need to check what a string encodes to — or decode a mangled one from a log file to see what actually got sent — a quick scratchpad beats writing a one-off script. I keep the URL Encoder / Decoder open for exactly that: paste, see the result, move on. It's especially handy for untangling double-encoded values where you need to decode a couple of times to find the original.

Rules of thumb to keep

  • Encode values, never whole URLs.
  • Use encodeURIComponent for query parameters, not encodeURI.
  • Prefer built-in tools like URLSearchParams or urllib.parse over hand-rolled concatenation.
  • Always work with UTF-8 bytes for non-ASCII text.
  • Encode exactly once; watch for %25 as the signature of double encoding.

None of this is complicated once you see the pattern: URLs have structure, and encoding exists to keep your data from being mistaken for that structure. Get the boundary right — the one place where you build the URL from parts — and the rest takes care of itself.

Photo by Markus Spiske on Unsplash