Percent-encoding is one of those things you half-understand until a query string blows up in production. A customer's name has a + in it, or a search term includes an ampersand, and suddenly your API is parsing garbage. Getting URL encoding right isn't hard, but it does require knowing which rules apply where.

What URL encoding actually 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, non-ASCII characters, and reserved symbols used for structure — has to be escaped.

Encoding replaces an unsafe character with a percent sign followed by the two-digit hexadecimal value of its byte(s). A space becomes %20. An ampersand becomes %26. The letter é in UTF-8 is two bytes, so it becomes %C3%A9. That's the whole mechanism.

The important detail: encoding operates on bytes, not characters. That's why your text encoding matters. Modern systems assume UTF-8, and you should too.

When you actually need it

You need encoding any time user-controlled or structured data goes into a URL. The most common cases:

  • Query string values — search terms, filters, redirect targets.
  • Path segments — slugs or IDs that might contain slashes or spaces.
  • Form submissions sent as application/x-www-form-urlencoded.
  • Building links programmatically where any part comes from outside your control.

If a value is hardcoded and you know it's safe, you don't need to encode it. The danger is dynamic data. Assume anything a user can type will eventually contain the one character that breaks your URL.

Reserved vs unreserved characters

This is where people get confused. Some characters are reserved because they have meaning in a URL's structure: ? starts the query, & separates parameters, / separates path segments, # marks a fragment. When one of these appears inside a value, it must be encoded so it isn't mistaken for structure.

Consider a redirect parameter pointing at another URL:

https://example.com/go?next=https://site.com/page?id=5&ref=x

That's broken. The ? and & in the next value get read as part of the outer URL. Encode the value and it holds together:

https://example.com/go?next=https%3A%2F%2Fsite.com%2Fpage%3Fid%3D5%26ref%3Dx

The gotchas that cause real bugs

encodeURI vs encodeURIComponent

In JavaScript, these two functions are not interchangeable. encodeURI() is for encoding a whole URL and deliberately leaves reserved characters like &, ?, and / alone. encodeURIComponent() is for encoding a single piece — a parameter value — and escapes those reserved characters too.

Use the wrong one and you'll either double-break a URL or fail to escape something that matters:

const term = "cats & dogs";

// Wrong: & survives, breaks the query
encodeURI(term);           // "cats%20&%20dogs"

// Right: & is escaped
encodeURIComponent(term);  // "cats%20%26%20dogs"

const url = `https://example.com/search?q=${encodeURIComponent(term)}`;

Rule of thumb: encode each value with encodeURIComponent, then assemble the URL yourself.

The plus-sign trap

In the query string of an application/x-www-form-urlencoded body, + means space. But in a path, + is a literal plus. This inconsistency bites people constantly. encodeURIComponent encodes space as %20, which is safe everywhere, but some server frameworks still decode + as space in query strings. If your data contains literal plus signs — think C++ or a phone number with a country code — encode them explicitly as %2B.

Double encoding

Encoding an already-encoded string turns %20 into %2520 because the % itself gets escaped. This usually happens when a value passes through two layers that both "helpfully" encode. The symptom is literal %20 showing up in your UI. Encode exactly once, and decode exactly once.

Encoding the whole URL

Never run a complete URL through encodeURIComponent. You'll escape the :// and the slashes and end up with an unusable string. Only encode the parts that need it.

A quick worked example

Say you're building a link to a help article and the title comes from a CMS: What's new in v2.0 (beta)?. You want it as a readable query parameter.

const title = "What's new in v2.0 (beta)?";
const encoded = encodeURIComponent(title);
// "What's%20new%20in%20v2.0%20(beta)%3F"

const link = `https://docs.example.com/go?title=${encoded}`;

Notice the ? at the end became %3F — exactly what we want, since otherwise it would start a second query section. The apostrophe and parentheses are left alone because they're legal in a query value. On the receiving end, your framework's query parser (or decodeURIComponent) reverses it cleanly.

When you just need to check what a string encodes or decodes to — debugging a logged URL, inspecting a redirect, or sanity-checking an API response — it's faster to paste it into a tool than to open a console. Our URL Encoder / Decoder handles both directions and shows the result instantly.

Server-side notes

Most backends do the right thing automatically. Express, Django, Rails and friends decode query parameters for you, so req.query.q already contains cats & dogs, not the escaped version. Problems arise when you read the raw URL string yourself or build requests by hand. If you're constructing an outbound URL in Node, prefer the URL and URLSearchParams APIs — they encode correctly without you thinking about it:

const url = new URL("https://example.com/search");
url.searchParams.set("q", "cats & dogs");
url.toString();
// "https://example.com/search?q=cats+%26+dogs"

Note that URLSearchParams uses + for spaces, which is valid form encoding. If you specifically need %20, you'll have to replace it manually.

The short version

Encode values, not whole URLs. Use encodeURIComponent for individual components. Watch for the plus-sign ambiguity and double encoding. Assume UTF-8. And when a URL looks wrong, decode it piece by piece instead of guessing — the escaped bytes always tell you exactly what happened.

Photo by Chris Ried on Unsplash