URL encoding (percent-encoding)
Why spaces and symbols in a URL get turned into %20 and friends, and the difference between query and path encoding.
What percent-encoding is
URLs may only contain a limited set of characters from the ASCII set. Any character outside that set β or a reserved character used for its literal value β is encoded as a % followed by two uppercase hex digits representing its byte.
hello world β hello%20world
c++ & c# β c%2B%2B%20%26%20c%23
price=β¬10 β price%3D%E2%82%AC10 (β¬ is 3 UTF-8 bytes: E2 82 AC)Why it exists
The URL grammar reserves characters such as ?, &, =, #, / for structure. If a value legitimately contains one of them, the parser must know it is data, not syntax. Encoding disambiguates.
%20 almost everywhere, but in the application/x-www-form-urlencoded body used by HTML forms a space becomes a +. That is a different rule for a different context.Encoding is over bytes, not characters
Modern URLs encode the UTF-8 bytes of the character, not the code point directly. Γ© (U+00E9) is one UTF-8 byte 0xC3 0xA9, so it becomes %C3%A9. This is why the same character always encodes the same way regardless of the platform.
// JavaScript
const s = 'cafΓ©';
const enc = encodeURIComponent(s); // "caf%C3%A9"
const dec = decodeURIComponent(enc); // "cafΓ©"FAQ
What is the difference between encodeURI and encodeURIComponent?
Why do I see + instead of %20?
Related
Base64 encoding UTF-8 and character sets
Last refreshed 2026-09-17.