URL encoding is one of those things you half-understand until a bug forces you to learn it properly. A link works fine in testing, then someone pastes an ampersand into a search box and suddenly your query string falls apart. Understanding what encoding actually does — and where it applies — saves a lot of head-scratching.
What URL encoding is
URLs are only allowed to contain a limited set of ASCII characters. Letters, digits, and a handful of symbols like -, ., _ and ~ are safe. Everything else — spaces, slashes in the wrong place, question marks, non-ASCII characters — has to be represented some other way.
That's what percent-encoding does. Each unsafe byte gets written as a % followed by its two-digit hexadecimal value. A space becomes %20. An ampersand becomes %26. The letter é, encoded as UTF-8, becomes %C3%A9 — two bytes, so two percent sequences.
The rules come from RFC 3986. The key idea is that some characters are reserved because they have structural meaning in a URL (like ?, &, /, #), and when you want those characters to be treated as data rather than structure, you encode them.
When you actually need it
You reach for URL encoding whenever you're putting arbitrary data into a URL. The most common cases:
- Query string values. Building
?q=...where the value might contain spaces, ampersands, or symbols. - Path segments. If a filename or slug contains a slash or space, it needs encoding so it isn't read as a new path part.
- Form submissions. HTML forms with
application/x-www-form-urlencodedencode their fields automatically. - Redirect and callback URLs. When one URL is passed as a parameter inside another (OAuth flows love this), the inner URL must be encoded.
Encode the parts, not the whole
The single biggest mistake is encoding an entire URL at once. If you run https://example.com/search?q=cats through an encoder, you'll turn the :, /, ? and = into percent sequences and break the whole thing.
Encode individual components — the value of each query parameter, each path segment — and then assemble the URL from those encoded pieces. The structural characters stay as they are.
JavaScript's two functions
JavaScript gives you encodeURI and encodeURIComponent, and the difference matters. encodeURI assumes you're passing a full URL and leaves reserved characters like & and = alone. encodeURIComponent assumes you're passing a single value and encodes almost everything.
const q = "cats & dogs";
encodeURI(q);
// "cats%20&%20dogs" -- the & is NOT encoded
encodeURIComponent(q);
// "cats%20%26%20dogs" -- the & becomes %26
const url = `https://example.com/search?q=${encodeURIComponent(q)}`;
// https://example.com/search?q=cats%20%26%20dogsRule of thumb: use encodeURIComponent for anything you're inserting as a value, and only use encodeURI if you somehow have a complete URL string you want to make safe.
The gotchas that bite people
Plus signs and spaces
This one is genuinely confusing. In the application/x-www-form-urlencoded format — used by HTML forms and many APIs — a space is encoded as +, not %20. But in a URL path, a + is a literal plus sign and spaces are %20.
So if you decode a query string with a generic URL decoder, a + might stay as a plus when you expected a space, or vice versa. When you're debugging form data, be explicit about which convention applies. In practice, encoding spaces as %20 is safe almost everywhere; relying on + is not.
Double encoding
If a value gets encoded twice, % itself becomes %25, so %20 turns into %2520. You'll see this when a framework encodes a parameter you already encoded by hand. The fix is to encode exactly once at the boundary where you build the URL, and let the receiving side decode exactly once.
Non-ASCII and UTF-8
Always encode text as UTF-8 before percent-encoding. Modern functions do this by default, but if you're working in an older language or dealing with Latin-1 data, mismatched character sets produce mojibake. The hex bytes you see should correspond to the UTF-8 representation of the character.
Characters that are safe but often encoded anyway
Some characters are technically legal in parts of a URL but are frequently encoded for safety — !, ', (, ), *. encodeURIComponent deliberately leaves these alone, which occasionally trips up systems that expect them encoded. If you hit this, encode them manually.
A worked example
Say you're building a link to a maps service and the address contains spaces, a comma, and an ampersand:
const address = "100 Main St, Smith & Co.";
const base = "https://maps.example.com/search";
const url = `${base}?address=${encodeURIComponent(address)}`;
// https://maps.example.com/search?address=100%20Main%20St%2C%20Smith%20%26%20Co.Notice that the comma became %2C and the ampersand %26. If you'd left the ampersand raw, the server would have read Co. as the start of a second parameter and dropped it entirely.
On the receiving end, decoding returns the original string:
decodeURIComponent("100%20Main%20St%2C%20Smith%20%26%20Co.");
// "100 Main St, Smith & Co."When you just need to check what an encoded string decodes to — or encode something quickly without opening a REPL — a browser-based URL Encoder / Decoder does the job in a second. It's handy for sanity-checking a suspicious parameter or spotting a double-encoding bug.
Quick checklist
- Encode values, not whole URLs.
- Use
encodeURIComponentfor query params and path segments. - Prefer
%20over+for spaces unless you're specifically building form-encoded data. - Encode once, decode once — watch for
%2520. - Make sure your text is UTF-8 before encoding.
Get these right and the vast majority of mysterious "my link breaks with certain input" bugs simply disappear. Encoding isn't glamorous, but it's the kind of fundamental that pays for itself every time you handle user input in a URL.
Photo by Ilya Pavlov on Unsplash