URLs can only contain a limited set of characters, and some of those have special meanings. Percent-encoding is how everything else, from spaces to accented letters and emoji, is carried safely inside a URL.

The allowed characters

The URL standard (RFC 3986) defines a set of unreserved characters that never need encoding: letters A–Z and a–z, digits 0–9, and - . _ ~.

Reserved characters have structural meaning: : / ? # [ ] @ ! $ & ' ( ) * + , ; =. For example, ? starts the query string, & separates parameters, and # starts a fragment. When one of these appears as data rather than structure, it must be encoded.

Everything else, including spaces, quotes, non-ASCII letters and emoji, must always be encoded.

How encoding works

  1. The character is converted to bytes using UTF-8.
  2. Each byte is written as % followed by two hexadecimal digits.
CharacterUTF-8 bytesEncoded
space20%20
&26%26
éC3 A9%C3%A9
😀F0 9F 98 80%F0%9F%98%80

Decoding reverses the process: collect the bytes and decode them as UTF-8.

Encoding a value versus a whole URL

This is the most common source of bugs.

  • Encoding a value (a search term, a parameter, a path segment) must also encode reserved characters, so that an & in the data can’t be mistaken for a parameter separator. In JavaScript this is encodeURIComponent.
  • Encoding a complete URL must leave its structure alone. encodeURI keeps : / ? & = # and encodes only characters that can never appear.

Build URLs by encoding each value separately, then assembling them. Encoding a finished URL with encodeURIComponent breaks it (https%3A%2F%2F…).

Spaces: %20 or +?

In HTML form submissions (application/x-www-form-urlencoded), spaces are encoded as +. In other parts of a URL, %20 is correct and + means a literal plus sign. Decoders need to know which convention the text uses; the URL Decoder lets you choose.

Double encoding

Encoding already-encoded text turns % into %25, so %20 becomes %2520. If you see %25 sequences in a URL, something was probably encoded twice.

Slugs versus encoding

Encoding preserves the original text exactly and is reversible. A slug rewrites a title into readable, unreserved characters and is not reversible. Use slugs for page addresses and encoding for data.