Every time a URL with a space, an apostrophe or an emoji breaks in production, percent-encoding is the culprit. Formally called percent-encoding, this scheme replaces characters that are unsafe in URLs with a percent sign followed by two hex digits: the space becomes %20, and the percent sign itself becomes %25. This guide covers the character rules, how the encoding actually works byte by byte, and the traps that trip up even experienced developers.
Why URLs cannot contain anything
A URL has structure: scheme, authority, path, query and fragment. Characters like :, /, ?, #, & and = carry meaning in that structure, and characters outside plain ASCII cannot safely travel across the internet. Percent-encoding solves both problems in one pass: reserve some characters for syntax, encode everything else, and encode the reserved characters when they are meant as data.
The character rules in one table
| Character | Encoded form | Why it matters |
|---|---|---|
| space | %20 | The most common bug in hand-built URLs |
| % | %25 | The percent sign must encode itself |
| & | %26 | Reserved: starts a new query parameter |
| = | %3D | Reserved: separates a parameter key from its value |
| ? | %3F | Reserved: begins the query string |
| # | %23 | Reserved: begins the fragment |
| / | %2F | Reserved: path separator — encode only as data |
| é | %C3%A9 | One UTF-8 character, two encoded bytes |
Letters A-Z, a-z, digits 0-9 and the marks - . _ ~ are unreserved and never need encoding.
How to encode a string by hand
Encode by converting each character to its UTF-8 bytes, then writing each byte outside the safe range as a percent sign plus its two uppercase hex digits. The string hello world contains one unsafe character — the space, byte 0x20 — so it becomes hello%20world. The string 100% off contains a bare percent and a space: 100%25%20off. The apostrophe in don't stop encodes as don%27t%20stop. Feed any string to the URL Encoder/Decoder to check your result.
The + vs %20 trap
In the query string (after the ?), web forms traditionally encode spaces as a literal +. So ?q=cake+recipe means the same as ?q=cake%20recipe in form data, but + inside a path means a literal plus sign, not a space. Most server frameworks and URL tools handle this for you, but it explains the classic bug set: a value decoded as text with plus signs where spaces should be, or a path segment where + was decoded as a space.
Common mistakes worth avoiding
- Encoding the whole URL. Never run encodeURIComponent on a complete address — it will turn the http:// and the slashes into data. Encode only the parts you built.
- Double-encoding. Encoding an already-encoded string turns %20 into %2520, which decodes to the literal text %20.
- Mixing encodeURI and encodeURIComponent. In JavaScript, encodeURI keeps reserved characters intact, while encodeURIComponent encodes them all. Using the wrong one corrupts query strings.
- Emoji and non-Latin text. A single emoji becomes several UTF-8 bytes, each percent-encoded — the safe length limit shrinks accordingly.
- Assuming the server and your script agree on UTF-8. If pages declare a legacy charset, encoded bytes can decode as mojibake.
When encodings collide: base64 inside a URL
Base64 output contains +, / and =, none of which survive a URL untouched — + means space in query strings, = and / are reserved. The fix is base64url, which replaces + with - and / with _, then strips the padding. The Base64 Encoder/Decoder shows both variants, so you can paste a value, flip modes and confirm the URL-safe form before building the link.
Encode the parts you actually ship
When a search page, redirect or canonical tag needs a correctly encoded address, generate it with the URL Encoder/Decoder and compare input versus output before committing. If the URL lives inside HTML markup rather than a browser bar, remember that HTML entities and percent-encoding are two separate layers. The Meta Tag Generator shows the distinction in practice — it writes canonical and og:url tags where both the ampersands and the URL values have to be handled correctly for the same page.
Frequently asked questions
Should I use encodeURI or encodeURIComponent? encodeURIComponent for any value you insert into a URL (query keys, values, paths you build); encodeURI for when you already have a complete, correct URL and merely want to coerce stray unsafe characters.
Is there a practical difference between %20 and +? Yes. %20 is correct everywhere. + is the form-encoding convention and means a space only inside query strings — in the path it is a literal plus. Prefer %20 unless you are mimicking an HTML form.
Do I need to encode every query parameter? Every value that can contain reserved characters, yes — at minimum spaces, ampersands, equals signs and non-ASCII text. Encoders exist precisely so you do not have to gamble on what a value might contain.