You've hit this bug. A user searches for C++ & Java, your app glues it onto a URL, and suddenly the backend only sees C. Or a redirect loses half its parameters. Nine times out of ten the culprit is url encoding — or the lack of it.
What URL encoding actually is
URLs are only allowed to contain a limited set of ASCII characters. Letters, digits, and a few punctuation marks like -, ., _, and ~ are safe. Everything else — spaces, ampersands, slashes in the wrong place, accented letters, emoji — has to be represented some other way.
That "other way" is percent-encoding. Each unsafe byte becomes 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.
That's the whole mechanism. The tricky part is knowing when and which characters need it.
Why characters are "reserved"
Some characters are dangerous not because they're exotic, but because they mean something structurally. In a URL, these have jobs:
?starts the query string&separates query parameters=splits a key from its value/separates path segments#starts the fragment+means a space (in query strings, historically)
If a user's actual data contains one of these, you have to encode it, otherwise the parser can't tell your data apart from the URL's structure. That's exactly why C++ & Java explodes: the & gets read as a parameter separator.
The encode-the-value, not-the-URL rule
The single most important habit: encode each piece of data before you assemble the URL, not the finished URL as a whole.
If you encode a fully-built URL, you'll mangle the :, //, and ? that are supposed to be there. Instead, encode the dynamic bits — the search term, the user ID, the filename — and then concatenate.
const term = 'C++ & Java';
const url = `https://example.com/search?q=${encodeURIComponent(term)}`;
// https://example.com/search?q=C%2B%2B%20%26%20JavaNote which function I used. This matters more than people expect.
encodeURI vs encodeURIComponent
JavaScript gives you two functions and they are not interchangeable.
encodeURIassumes you're handing it a whole URL, so it leaves structural characters like?,&,/, and:alone.encodeURIComponentassumes you're handing it a single value, so it encodes nearly everything, including&and=.
For query parameter values and path segments, you almost always want encodeURIComponent. Reach for encodeURI only when you genuinely have a complete URL and want to tidy up spaces and non-ASCII characters without breaking the structure.
Common gotchas
The plus sign problem
In the application/x-www-form-urlencoded format — what HTML forms submit and what many frameworks parse — a space is encoded as +, not %20. But in a regular URL path, + is a literal plus. This mismatch causes genuine data corruption: someone submits a+b, and your server reads a b.
If you need reliable round-tripping, encode spaces as %20 and encode literal pluses as %2B. Don't rely on + meaning a space unless you control both ends and know the format.
Double encoding
Encode something twice and %20 becomes %2520 — because the % itself gets encoded into %25. You'll see this when a value passes through several layers (a client, a proxy, a framework) that each helpfully encode it. The symptom is literal percent sequences showing up in your UI. The fix is to find out who's already doing the encoding and stop everyone else from doing it again.
Encoding before you've assembled the structure
Related trap: building the query string by hand, encoding it, then handing it to a library that encodes it again. Pick one layer to own the encoding.
Non-ASCII and the UTF-8 assumption
Percent-encoding works on bytes, not characters. Modern practice is to encode text as UTF-8 first, then percent-encode those bytes. encodeURIComponent does this correctly. Older systems sometimes used other encodings, which is why decoding a legacy URL occasionally produces garbled text.
A quick worked example
Say you want to build a link that filters a report by a customer name containing special characters, plus a date range.
const name = 'Smith & Sons, Ltd.';
const from = '2024-01-01';
const to = '2024-03-31';
const params = new URLSearchParams({
customer: name,
from,
to,
});
const url = `https://reports.example.com/filter?${params}`;
// ...?customer=Smith+%26+Sons%2C+Ltd.&from=2024-01-01&to=2024-03-31Using URLSearchParams handles the encoding for you and keeps the separators correct — which is why I prefer it over string concatenation for anything with more than one parameter. Notice it uses + for spaces, which is fine here because it's producing form-url-encoded output that the matching parser expects.
When you need to eyeball what a string becomes, or decode something a server logged, a scratchpad beats writing throwaway code. I keep Devsly's URL Encoder / Decoder open for exactly that — paste a value, see the encoded form, flip it back to check round-tripping, and spot double-encoding instantly.
A short checklist
- Encode individual values, not the whole URL.
- Use
encodeURIComponentfor query values and path segments. - Decide whether spaces are
%20or+and be consistent with your parser. - Make exactly one layer responsible for encoding to avoid double encoding.
- Encode text as UTF-8 before percent-encoding.
- Prefer
URLSearchParams(or your language's equivalent) over hand-built query strings.
Most url encoding bugs aren't really about the encoding — they're about two parts of the system disagreeing on who does it and in what format. Nail down that contract once and this class of bug largely disappears.
Photo by Chris Ried on Unsplash