learn

What is URL encoding (percent-encoding)?

Why spaces become %20 or +, which characters must be encoded, encodeURIComponent vs encodeURI, UTF-8 bytes like %C3%A7 and how double encoding happens.

Why URLs need encoding

A URL may only contain a small set of ASCII characters, and some of them have a job: / separates path segments, ? starts the query, & separates parameters, # starts the fragment. Any other character, a space, an accent, an emoji, or a reserved character used as plain data, must be percent-encoded: written as % followed by the two hexadecimal digits of each of its bytes in UTF-8. This is defined by RFC 3986, and it is why café in a link becomes caf%C3%A9.

Which characters are encoded

GroupCharactersIn a URL
UnreservedA–Z a–z 0–9 - . _ ~never encoded
Reserved: / ? # [ ] @ ! $ & ' ( ) * + , ; =encoded when used as data
Everything elsespace, %, ", <, >, accents, emoji…always encoded

Non-ASCII text is first turned into UTF-8 bytes, then each byte is written as %XX: ç is the two bytes C3 A7, so it becomes %C3%A7; € is three bytes, %E2%82%AC. See what is UTF-8 for why. The URL encoder and the URL decoder do both directions.

Space: %20 or +?

Both exist. In a path, a space is always %20. In the query of an HTML form (application/x-www-form-urlencoded), a space is written +, and a real plus sign must be %2B. JavaScript's URLSearchParams follows the form rule:

new URLSearchParams({ q: 'café com leite' }).toString();
// 'q=caf%C3%A9+com+leite'
encodeURIComponent('ação & café?');
// 'a%C3%A7%C3%A3o%20%26%20caf%C3%A9%3F'

A server that decodes + as a space will turn C++ into C if the plus signs were not encoded.

encodeURIComponent vs encodeURI

  • encodeURIComponent encodes everything except unreserved characters and ! * ' ( ). Use it for one value: a query parameter, a path segment.
  • encodeURI leaves the URL structure alone (: / ? # & = + stay as they are). Use it only to clean up a whole URL that is already well-formed.

Using encodeURI on a parameter value is a classic bug: an & inside the value splits the parameter in two.

Double encoding and other traps

  • Double encoding: encoding an already encoded value turns %20 into %2520 (% itself becomes %25). Decode once with the URL decoder to see the original.
  • Decoding too early: decoding a full URL before splitting it can turn an encoded %2F into a real / and change the path.
  • Encoding is not escaping for HTML: inside HTML attributes use HTML entities; URL encoding does not protect against injection.

To see each part of a long link, paste it in the URL parser.

FAQ

What does %20 mean?

It is the space character, byte 0x20, percent-encoded. %2F is /, %3F is ?, %26 is &.

Is URL encoding the same as Base64?

No. URL encoding keeps readable characters and escapes only the unsafe ones; Base64 rewrites all bytes with 64 characters.

Why do accented letters take six characters?

Because UTF-8 uses two bytes for most accented Latin letters, and each byte takes three characters (%C3%A9).

Should I encode the whole URL?

No. Encode each value separately when you build the URL, then join the parts with the reserved characters.