What is URL Encoding (Percent-Encoding)?
Uniform Resource Identifiers (URIs) are constrained by the internet standard RFC 3986 to a limited subset of US-ASCII characters. Characters outside this character set—including whitespace, non-Latin alphabets, emojis, and characters with structural syntactic roles (such as ?, &, =, and /)—cannot be transmitted directly inside URL query parameters or path segments without ambiguity.
Percent-encoding resolves this conflict by translating non-allowed or reserved characters into a three-character sequence: a percent symbol (%) followed by two hexadecimal digits representing the byte's numeric value in hexadecimal (base 16). For multi-byte UTF-8 sequences (such as accented letters or emojis), each consecutive byte is converted into its own %XX pair.
Reserved vs. Unreserved Characters: The RFC 3986 Specification
Understanding which characters require encoding depends on whether they belong to the unreserved or reserved groups:
| Category | Characters Included | Encoding Rule |
|---|---|---|
| Unreserved | A–Z, a–z, 0–9, -, _, ., ~ | Never encoded; safe in any part of a URL. |
| Reserved (Delimiters) | : / ? # [ ] @ | Delimit URL structural components (scheme, authority, path, query, fragment). |
| Sub-delimiters | ! $ & ' ( ) * + , ; = | Separate key-value pairs or query arguments (e.g., & and =). |
Critical Distinction: encodeURI vs. encodeURIComponent
In JavaScript and modern web applications, developers frequently encounter bugs by selecting the wrong encoding function:
encodeURI(): Intended for a complete URL (e.g.,https://example.com/search?q=hello world). It preserves protocol colons, slashes, and question marks (: / ? # & =), only encoding spaces and non-ASCII characters.encodeURIComponent(): Intended for individual query parameter keys and values (e.g., passing a redirect URL likereturnTo=https://mysite.com/login?ref=123). It encodes slashes, colons, ampersands, and question marks so they do not break the enclosing URL structure.
Common URL Encoding Examples & Byte Translations
| Plain Character | Hex / Percent-Encoded | Technical Reason |
|---|---|---|
| Space ( ) | %20 (or + in forms) | Whitespace is invalid in URLs and truncates addresses in browsers. |
| Ampersand (&) | %26 | Separates query parameters; unencoded ampersands inject unintended keys. |
| Equals (=) | %3D | Delimits parameter names from values. |
| Forward Slash (/) | %2F | Delimits path segments; must be escaped when passed as data inside a query. |
| Question Mark (?) | %3F | Marks the start of the query string. |
| Hash (#) | %23 | Delimits URL hash fragments (browser anchor points). |
Real-World Bugs Prevented by Proper URL Encoding
- OAuth & Single Sign-On (SSO) Redirects: Callback URLs passed as query parameters (e.g.
redirect_uri=https://app.com/callback?session=xyz) must have their inner parameters encoded, otherwise the authorization server will parsesession=xyzas its own parameter instead of forwarding it. - Search Engine Optimization (SEO) & Crawling: Clean, properly encoded URLs prevent search bot crawlers from tripping over rogue spaces or unencoded non-ASCII characters, ensuring canonical URLs index correctly in Google Search Console.
- API Data Integrity: When passing email addresses containing
+(e.g.,user+test@domain.com), many HTTP web servers automatically convert+to a space. Encoding the plus sign as%2Bguarantees the server receives the exact literal address.
Frequently Asked Questions
Need to encode binary files or binary payloads into text?
Try Base64 Encoder / Decoder