URL encoding is one of those things everyone assumes they understand until a query string full of spaces and ampersands blows up in production. The rules aren't complicated, but they're easy to apply in the wrong place — or forget entirely. Here's a practical rundown.
What URL encoding actually is
URLs can only contain a limited set of characters safely. The spec (originally RFC 3986) reserves certain characters for structural meaning — things like ?, &, /, #, and =. Everything else that isn't in the "unreserved" set needs to be escaped so it doesn't get misinterpreted.
Percent-encoding does this by replacing a character with a % followed by its byte value in hexadecimal. A space becomes %20. An ampersand becomes %26. The letter é, encoded as UTF-8, becomes %C3%A9 — two bytes, two escape sequences.
The unreserved characters you never need to encode are the alphanumerics plus - _ . ~. Everything else is fair game depending on context.
When you actually need it
You need encoding any time user-supplied or dynamic data lands inside a URL. Common cases:
- Query string values — search terms, filters, anything a user types.
- Path segments built from arbitrary data, like a username or a file name.
- Redirect targets passed as a parameter (e.g.
?return_to=...). - OAuth and signing flows, where exact byte-for-byte encoding matters for signature validation.
The classic failure: you build a search link like /search?q=fish & chips and wonder why the server only sees fish. The space breaks the URL and the & starts a brand-new parameter. Encode the value and it works: /search?q=fish%20%26%20chips.
The gotcha that catches everyone: component vs. whole URL
This is the single most common mistake. There are two different jobs:
Encoding a component
When you have a value that goes inside a URL — a single query parameter, one path segment — you want aggressive encoding that escapes &, =, /, ? and friends. In JavaScript that's encodeURIComponent.
Encoding a whole URL
When you already have a full URL and just want to clean up spaces or non-ASCII, you want the gentler encodeURI, which leaves structural characters intact so the URL still works.
const value = "fish & chips/half";
encodeURIComponent(value);
// "fish%20%26%20chips%2Fhalf" ✅ safe inside ?q=
encodeURI(value);
// "fish%20&%20chips/half" ❌ & and / untouched — breaks as a valueRule of thumb: if you're assembling a URL from parts, encode each part with encodeURIComponent, then join them. Never encode the whole thing after you've already stuck the pieces together — you'll double-encode or miss the structural chars.
Double-encoding: the bug that hides for weeks
Double-encoding happens when something gets encoded twice. A % from the first pass gets escaped into %25 on the second pass. Suddenly %20 becomes %2520, and your server either stores the literal text "%20" or rejects the request.
This usually creeps in when multiple layers each try to be helpful — your code encodes, then an HTTP client encodes again, then a proxy normalizes. The fix is discipline: encode exactly once, at the point where you build the URL, and let everything downstream pass it through untouched.
// Wrong — the URLSearchParams already encoded it
const p = new URLSearchParams({ q: "a&b" }); // q=a%26b
const url = "/search?" + encodeURIComponent(p.toString());
// "q%3Da%2526b" ← double-encoded, broken
// Right
const url = "/search?" + p.toString(); // "/search?q=a%26b"Use URLSearchParams or URL when you can. They handle encoding correctly and stop you reinventing it by hand.
The plus-sign trap
In the application/x-www-form-urlencoded format — what HTML forms submit and what many query strings use — a space is encoded as +, not %20. But in a URL path, + is a literal plus sign. So %2B means a real plus, and + in a query string means a space.
This bites when you take a value from a query string and reuse it in a path without re-normalizing. A password or token containing + is a frequent victim. If you see mystery spaces where there should be plus signs, this is almost always the culprit.
A quick worked example
Say you're building a link to a maps service with an address that contains commas, spaces, and an ampersand in the business name:
const place = "Joe's Coffee & Tea, 10 High St";
const url = "https://example.com/search?q="
+ encodeURIComponent(place);
// https://example.com/search?q=Joe's%20Coffee%20%26%20Tea%2C%2010%20High%20StEvery structural character is now escaped: the space is %20, the ampersand is %26, the comma is %2C. The server receives one clean q value and nothing gets split or dropped.
When you need to eyeball what a string will look like encoded — or decode a gnarly URL someone pasted into a bug report — it's quicker to use a tool than to open a console. I keep Devsly's URL Encoder / Decoder open in a tab for exactly that: paste in a value, see the encoded result, and reverse it instantly to sanity-check what a server actually received.
Quick checklist
- Encode values, not whole assembled URLs.
- Use
encodeURIComponentfor parameters and path segments,encodeURIfor full URLs you only need to tidy. - Prefer
URLSearchParams/URLso you don't hand-roll escaping. - Encode exactly once — watch for double-encoding across layers.
- Remember
+means space in form-encoded query strings but a literal plus in paths. - Everything is UTF-8 bytes under the hood; non-ASCII expands to multiple
%XXsequences.
Most encoding bugs come down to doing it in the wrong place or doing it twice. Get those two habits right and the rest takes care of itself.
Photo by Florian Olivo on Unsplash