URL encoding explained
A URL accepts only a small set of characters. Here is how the rest is encoded, and the mistakes that break links.
Why URLs need encoding
A URL, as defined by RFC 3986, contains only a small set of ASCII characters. Some of them structure the address;
others have no place in it: spaces, accented letters, quotation marks, angle brackets. To carry them anyway, they are
replaced by a % sign followed by two hexadecimal digits. This is percent-encoding,
better known as URL encoding.
Unreserved and reserved characters
RFC 3986 sorts the allowed characters into two families:
| Family | Characters | Role |
|---|---|---|
| Unreserved | A–Z a–z 0–9 - . _ ~ | Always written as is. |
| Reserved, general delimiters | : / ? # [ ] @ | Split the address into scheme, host, path, query and fragment. |
| Reserved, sub-delimiters | ! $ & ' ( ) * + , ; = | Separate parameters and their values, like & and = in the query. |
A reserved character means something where it stands. To use it as plain data, for instance the &
in “Smith & Sons”, you encode it (%26); otherwise it cuts the value short. Everything else, spaces,
accents, emoji, control characters, is always encoded.
Byte by byte, in UTF-8
Percent-encoding applies to bytes, not characters. A character is first converted to UTF-8, then each of its bytes
becomes %XX:
space 20 → %20
é C3 A9 → %C3%A9
€ E2 82 AC → %E2%82%AC
Browsers often show the decoded address in the address bar, but send the encoded form to the server. An older
application that encodes in Latin-1 writes é as %E9, a sequence a UTF-8 decoder rejects:
a frequent cause of characters replaced by �.
encodeURI or encodeURIComponent
JavaScript offers two functions, which are the two modes of the kit's URL encoder and decoder:
encodeURIComponentencodes everything except letters, digits and- _ . ! ~ * ' ( ). Use it for a value inserted into an address: a parameter, a path segment.encodeURIalso keeps the separators; , / ? : @ & = + $ #. Use it for a full address whose structure must stay intact.
The classic mistake is to encode a value with encodeURI:
const company = 'Smith & Sons';
'https://example.com/?company=' + encodeURI(company)
// https://example.com/?company=Smith%20&%20Sons
// the server reads company = "Smith" and a second parameter "Sons"
'https://example.com/?company=' + encodeURIComponent(company)
// https://example.com/?company=Smith%20%26%20Sons
The safest option is to let a library build the query: URLSearchParams in JavaScript,
Uri.EscapeDataString in C#, rawurlencode in PHP, urllib.parse.quote(value,
safe='') in Python.
The “+” of HTML forms
An HTML form submitted with GET, or with POST without files, uses the
application/x-www-form-urlencoded format. This format, older than RFC 3986, writes a space as
+ and encodes a real plus sign as %2B:
q=caf%C3%A9+cr%C3%A8me+%26+co
URLSearchParams, PHP's urlencode and Python's urllib.parse.urlencode produce
this format. To decode it, each + becomes a space before the %XX are decoded. A
+ written as is in a value is therefore read as a space by most servers: a phone number
+441632960000 arrives as “441632960000” preceded by a space. Encoded with
encodeURIComponent, it becomes %2B441632960000 and arrives intact.
Double encoding
Encoding a value that is already encoded turns its % into %25: %20 becomes
%2520. You then see %20 in a title or a file name, or %2520 in the logs. The
cause is almost always the same: two layers that each encode, for instance a framework and a manual call. The rule:
encode once, when building the address, from the raw values.
Repeated decoding deserves the same care. A value such as %252e%252e%252f, decoded twice, becomes
../: a security check applied between the two decodings misses it. A server decodes once, then
validates the resulting value.
Keep secrets out of URLs
Encoded or not, an address is made to be read, copied and kept. A password or a token placed in a parameter ends up:
- in the browser history, and in its sync across devices;
- in the access logs of web servers, proxies and load balancers, which record the full address, query included;
- in the
Refererheader, which passes all or part of the address to the sites the page links to or loads resources from; - in copies, screenshots and link previews generated by messaging apps.
HTTPS encrypts the address in transit, not in these places where it is stored. A secret travels in a header, such
as Authorization, or in the body of a POST request. To share a log or an address that contains one,
the secret redaction tool replaces it before you send it.
Sharing a secret found in an address
If decoding reveals a password or a token, hand it over separately, outside any address. With “Send with onlyU”, the decoder passes the result to onlyU from one tab to another in your browser, without putting it in an address or routing it through the kit's server. onlyU encrypts it in your browser and creates a link your recipient opens only once, valid for 24 hours, after which the link disappears. It is free, with no sign-up.