URL encoding is one of those things you think you understand until a query parameter with a + in it silently breaks in production. It's simple on the surface — swap unsafe characters for percent codes — but the details around what's "unsafe", which encoding to use, and how different components of a URL behave are where the bugs live.
What URL encoding actually is
A URL can only contain a limited set of ASCII characters safely. The spec (RFC 3986) defines a small group of reserved characters that have special meaning — things like /, ?, #, &, = — plus a set of unreserved characters (letters, digits, and -, ., _, ~) that are always safe.
Everything else needs to be percent-encoded: the byte is written as a % followed by its two-digit hexadecimal value. A space becomes %20, an at sign becomes %40, and so on. For non-ASCII characters, the string is first encoded to UTF-8 bytes, then each byte is percent-encoded. That's why an em dash turns into three percent codes.
café → caf%C3%A9
hello world → hello%20worldWhen and why you'd use it
You encode whenever you're putting arbitrary data into a position in a URL where it could be misread as structure. The classic cases:
- Query parameters. A user searches for
C++ & Java. Drop that raw into?q=C++ & Javaand the&starts a new parameter, the spaces break the URL, and the+may be read as a space. Encode it toq=C%2B%2B%20%26%20Java. - Path segments. A filename like
report 2024/final.pdfcontains a slash that would be mistaken for a path separator. - Redirect URLs passed as parameters. Putting a full URL inside another URL (
?next=https://...) absolutely requires encoding, or the inner query string merges with the outer one. - OAuth and signed requests. Signatures are computed over encoded values, so getting the encoding exactly right matters for the signature to validate.
The gotchas that actually catch people
Spaces: %20 vs +
This is the big one. In the path and most of a URL, a space is %20. But in application/x-www-form-urlencoded data — which is what query strings often use — a space is encoded as +. So ?q=a+b and ?q=a%20b frequently mean the same thing, but not always, and a decoder that's strict about one form will mangle the other. Know which convention your server expects.
Encoding component-by-component, not the whole URL
Do not take a fully formed URL and run it through an encoder. That will escape the : and / in https:// and destroy the URL. Encode the individual pieces — each path segment, each parameter name and value — and then assemble them. In JavaScript this is the difference between encodeURI (preserves reserved structural characters) and encodeURIComponent (escapes almost everything). For values, you almost always want encodeURIComponent.
const base = "https://api.example.com/search";
const q = "C++ & Java";
const url = `${base}?q=${encodeURIComponent(q)}`;
// https://api.example.com/search?q=C%2B%2B%20%26%20JavaDouble-encoding
Encode something twice and %20 becomes %2520 (the % itself gets encoded to %25). This happens a lot when a value passes through several layers — a frontend encodes it, a framework encodes it again, and suddenly your filenames have literal %20 text in them. If you see %25 showing up unexpectedly, you've encoded one time too many.
Reserved characters inside values
Characters like &, =, #, and ? are legal in a URL, so a naive encoder might leave them alone — but inside a value they'll break parsing. encodeURIComponent handles this correctly; encodeURI does not. This trips people who reach for the wrong function.
Fragments and the hash
Everything after # is the fragment and is never sent to the server. If you stuff data after a hash expecting the backend to read it, it won't. That's a different problem from encoding, but it surfaces when people try to encode their way out of it.
A short worked example
Say you're building a share link that opens a pre-filled email with a subject and body. The mailto: scheme uses query-string style parameters, so the values must be encoded:
const subject = "Q3 report & numbers";
const body = "Here's the link: https://example.com/r?id=42";
const mailto = `mailto:[email protected]?subject=${encodeURIComponent(subject)}&body=${encodeURIComponent(body)}`;
// mailto:[email protected]?subject=Q3%20report%20%26%20numbers&body=Here's%20the%20link%3A%20https%3A%2F%2Fexample.com%2Fr%3Fid%3D42Notice the inner URL's :, /, and ? all got escaped (%3A, %2F, %3F). That's exactly right — they're data here, not structure, so they must not be interpreted as part of the mailto: URL.
When you're debugging something like this and just need to eyeball what a string encodes or decodes to, paste it into the URL Encoder / Decoder. It's faster than spinning up a REPL, and it's handy for spotting double-encoding at a glance — if the decode doesn't fully clean up on the first pass, you know something upstream encoded twice.
Quick rules to keep you out of trouble
- Encode values, not whole URLs. Assemble from encoded parts.
- Use
encodeURIComponentfor parameter values and path segments; useencodeURIonly when you already have a structurally valid URL. - Decide early whether spaces are
%20or+and stay consistent with your server. - If you spot
%25where you expected%, suspect double-encoding. - Non-ASCII? Make sure you're encoding UTF-8 bytes, which modern functions do by default.
Percent-encoding has been stable for decades, so the knowledge pays off indefinitely. Get the component-by-component habit right and most of the classic bugs simply stop happening.
Photo by Pankaj Patel on Unsplash