URL encoding is one of those things you half-understand until a bug forces you to actually learn it. A customer's name with an ampersand breaks a redirect, a search query with a # silently drops half its characters, and suddenly you're staring at a string full of %20 and %2F trying to work out what went wrong.
Let's clear it up.
What URL encoding actually is
URLs are only allowed to contain a limited set of characters: letters, digits, and a handful of punctuation marks like -, _, . and ~. Everything else — spaces, slashes used as data, question marks, unicode characters, emoji — has to be represented using percent-encoding.
The rule is simple: take the byte value of the character, and write it as a percent sign followed by its two-digit hexadecimal code. A space becomes %20. An ampersand becomes %26. The letter é, which is two bytes in UTF-8, becomes %C3%A9.
"hello world" -> hello%20world
"a&b" -> a%26b
"café" -> caf%C3%A9That's the whole mechanism. The hard part isn't the encoding itself — it's knowing when and which part of a URL to encode.
When you need it
You need encoding whenever user-controlled or dynamic data gets placed into a URL. The classic cases:
- Query string values — search terms, filters, tokens, email addresses.
- Path segments — slugs, IDs, or filenames that might contain spaces or special characters.
- Form submissions sent as
application/x-www-form-urlencoded. - OAuth and signing flows, where an incorrectly encoded parameter breaks the signature entirely.
The reason is reserved characters. Characters like ?, &, =, # and / have structural meaning in a URL. If your data contains them unencoded, the parser treats them as delimiters rather than content. Say you build a redirect like this:
/redirect?next=/dashboard?tab=billing&user=42The parser reads next=/dashboard?tab=billing and then a separate parameter user=42, which was never your intent. The fix is to encode the value:
/redirect?next=%2Fdashboard%3Ftab%3Dbilling%26user%3D42The gotchas that cause real bugs
1. Encoding the whole URL vs. just a component
This is the big one. There are two different jobs here, and most languages give you two different functions. In JavaScript:
encodeURI()encodes a full URL but leaves?,&,=,/and#alone, because they're structural.encodeURIComponent()encodes everything that isn't safe, including those delimiters.
Rule of thumb: use encodeURIComponent() on each value you drop into a URL, and never on the URL as a whole.
const q = "beer & pretzels";
const url = `/search?q=${encodeURIComponent(q)}`;
// -> /search?q=beer%20%26%20pretzels2. Spaces: %20 vs. +
In a URL path, a space is %20. But in the query string of a form submission (application/x-www-form-urlencoded), a space is historically encoded as +. This inconsistency bites people constantly. If you decode a+b expecting a b but use a path decoder, you'll get the literal a+b back. Know which context you're in.
3. Double encoding
If you encode an already-encoded string, %20 becomes %2520 (the % itself gets encoded to %25). This usually happens when a value passes through multiple layers — a client encodes it, then a framework encodes it again. The symptom is literal %20 text appearing on the page. When you see %25 where you didn't expect it, suspect double encoding.
4. Plus signs in data
A literal + in a query value — common in base64 strings and phone numbers — can be silently interpreted as a space on the server. Always encode it to %2B when it's actual data.
5. Unicode and byte order
Percent-encoding works on bytes, not characters. Modern URLs assume UTF-8. If any part of your stack uses a different encoding (looking at you, legacy systems), multi-byte characters will mangle. Standardise on UTF-8 end to end.
A short worked example
Suppose you're building a share link that passes a title and a callback URL:
const title = "Rust & Go: a comparison";
const callback = "https://example.com/done?id=99";
const params = new URLSearchParams({
title: title,
callback: callback,
});
const shareUrl = `https://share.io/new?${params.toString()}`;Using URLSearchParams here is the move — it handles encoding for every value automatically, so you don't have to remember to wrap each one in encodeURIComponent(). The result:
https://share.io/new?title=Rust+%26+Go%3A+a+comparison&callback=https%3A%2F%2Fexample.com%2Fdone%3Fid%3D99Notice the & and ? and : inside the callback are all encoded, so they can't break the outer query string. That's exactly what you want.
Debugging encoded strings quickly
When you're staring at a wall of %-escapes in a log line or a webhook payload, the fastest way to sanity-check it is to decode it and read it in plain text. I keep the URL Encoder / Decoder open for exactly this — paste in the raw value, decode it, and confirm you're seeing the string you expect (and not a doubly-encoded mess). It's also handy for the reverse: encoding a test value before you paste it into a curl command.
Quick checklist
- Encode values, not whole URLs — reach for
encodeURIComponent()orURLSearchParams. - Watch the space problem:
%20in paths,+in form-encoded query strings. - If you see
%25you didn't put there, you've double-encoded. - Encode literal
+in data to%2B. - Keep UTF-8 consistent across your whole stack.
None of this is complicated once the mental model clicks: URLs have structure, and encoding is how you smuggle arbitrary data past that structure without breaking it. Get that right and a whole category of nasty, intermittent bugs just disappears.
Photo by Valery Sysoev on Unsplash