Spaces turning into %20 or +, ampersands breaking your query string, a cheeky # silently chopping off half a URL — if you've shipped web software for any length of time, you've been bitten by URL encoding. It's one of those topics that feels trivial until a production bug forces you to actually understand it.

What URL encoding actually is

URLs are only allowed to contain a limited set of ASCII characters. The spec (RFC 3986) splits characters into unreserved ones that are always safe, and reserved ones that have special meaning in certain positions — things like /, ?, #, &, and =. Everything else, including spaces, non-ASCII letters, and most punctuation, has to be escaped.

The escaping scheme is called percent-encoding. Each byte that can't appear literally gets replaced by a % followed by its two-digit hexadecimal value. A space is byte 0x20, so it becomes %20. The copyright symbol © is 0xC2 0xA9 in UTF-8, so it becomes %C2%A9.

That's the whole idea. The confusion comes from when and what to encode, not from the mechanism itself.

When you actually need it

You encode whenever you're putting arbitrary data into a URL where that data could contain reserved or unsafe characters. The common cases:

  • Query string values — a search term like coffee & cream needs the ampersand escaped or it looks like a parameter separator.
  • Path segments — slugs or IDs that might contain slashes or spaces.
  • Form submissions — application/x-www-form-urlencoded bodies use the same scheme (with one twist, below).
  • Redirect targets — when you pass one URL as a parameter inside another, the inner URL must be fully encoded.

The rule of thumb: if a human or another system supplied the value, encode it before it touches the URL.

The gotchas that cause real bugs

1. Encoding the whole URL instead of the parts

This is the big one. You should never run an entire assembled URL through an encoder — that would mangle the :// and the ? and & separators you actually need. Encode each component before you concatenate.

2. encodeURI vs encodeURIComponent in JavaScript

JavaScript gives you two functions and picking the wrong one is a classic mistake. encodeURI leaves reserved characters like &, =, and / untouched because it assumes you're encoding a full URL. encodeURIComponent escapes those too, which is what you want for a single value.

const term = "coffee & cream";

encodeURI(term);          // "coffee%20&%20cream"  (ampersand survives!)
encodeURIComponent(term); // "coffee%20%26%20cream"   (correct for a param)

Use encodeURIComponent for individual query values and path segments. Use encodeURI only when you have a complete URL that's already structured correctly and just needs spaces and non-ASCII cleaned up.

3. Space: %20 or +?

In the path and in generic percent-encoding, a space is %20. In application/x-www-form-urlencoded data — the classic form-POST format and the query-string convention many frameworks follow — a space is often encoded as +. Both are valid in their contexts, but mixing them up leads to literal plus signs showing up where spaces should be. If you ever see John+Smith rendered as John+Smith in your UI, this is why: something decoded with the wrong ruleset.

4. Double encoding

Encode a value twice and %20 becomes %2520 (because the % itself gets escaped to %25). This usually happens when a value is encoded on the client, passed through a layer that encodes again, or logged and replayed. If you spot stray %25 sequences, you've got a double-encoding bug somewhere in the chain.

5. The fragment and the #

Everything after a # is the fragment and never gets sent to the server. If user data containing a # ends up unencoded in a URL, the server silently receives a truncated value. Always encode it.

A short worked example

Say you're building a share link that points to a search results page, and the search query itself contains special characters:

const base = "https://example.com/search";
const query = "size >= 10 & color=red";

const url = `${base}?q=${encodeURIComponent(query)}`;
// https://example.com/search?q=size%20%3E%3D%2010%20%26%20color%3Dred

Notice that >, =, and & inside the value are all escaped, so the server reads the entire string as the value of q rather than interpreting the embedded & as a new parameter. Decoding it on the other side gives you the original string back cleanly.

On the server, most frameworks decode query parameters automatically, so req.query.q (or your language's equivalent) already holds size >= 10 & color=red. You only decode manually when you're pulling raw values out of a URL string yourself.

Quick manual checks

When I'm debugging an encoding issue I rarely trust my mental hex table. Pasting the string into a tool and watching exactly what comes out is faster and more reliable. For one-off checks — verifying what a value should look like, or decoding a suspicious log entry to see if it's double-encoded — a browser-based URL Encoder / Decoder does the job without reaching for a REPL.

The mental model worth keeping

Boil it down to three habits and most URL bugs disappear:

  • Encode components, never whole URLs.
  • Pick the right function — encodeURIComponent for values, encodeURI for assembled URLs — and know whether your context wants %20 or + for spaces.
  • Encode once, at the point where structured data becomes a string, and decode once when it comes back.

URL encoding isn't complicated, but it's unforgiving. Get the component boundaries right and the rest tends to take care of itself.

Photo by Chris Ried on Unsplash