What Is URL Encoding? A Guide to Percent-Encoding in Web Addresses
URL encoding (percent-encoding) is the mechanism that ensures web addresses travel safely across the internet by converting special characters into a universally transmittable format. Every web developer encounters it daily.
Why URL Encoding Exists
URLs were originally designed to contain only a limited set of characters from the ASCII repertoire. However, the modern web needs to transmit data that includes spaces, accented characters, emojis, and symbols that have no place in a raw URL. URL encoding solves this problem by replacing unsafe characters with a % followed by two hexadecimal digits.
How Percent-Encoding Works
Each character that needs encoding is converted to its byte value (in UTF-8 for most modern systems) and represented as %XX, where XX is the hexadecimal value. For example:
- A space becomes
%20 - An ampersand
&becomes%26 - The euro sign
ā¬becomes%E2%82%AC(three bytes in UTF-8) - A forward slash
/becomes%2F
Some characters are always safe and never need encoding: letters (A-Z, a-z), digits (0-9), and a few special characters like -, _, ., and ~.
Reserved vs Unreserved Characters
RFC 3986 defines two categories:
- Unreserved characters: Letters, digits,
-,_,.,~ā these never need encoding. - Reserved characters:
:,/,?,#,[,],@,!,$,&,',(,),*,+,,,;,=ā these have structural meaning in URLs and must be encoded when used as data rather than delimiters.
When You Must Encode URLs
Common scenarios that require URL encoding:
- Query parameters: Values in
?key=valuepairs must be encoded to handle spaces and special characters. - Path segments: File names or slugs containing non-ASCII characters need encoding.
- Form submissions: HTML forms with
application/x-www-form-urlencodeduse a slightly different encoding where spaces become+. - Redirects: Server-side redirects with user-generated content in the URL.
Common Pitfalls
- Double encoding: Encoding an already-encoded URL produces
%25sequences that break the original data. - Encoding the entire URL: You should encode individual components (path, query values), not the whole URL including
://and?. - Plus sign confusion: In query strings,
+represents a space inapplication/x-www-form-urlencoded, but a literal+in the URL path.
Frequently asked questions
What is the difference between URL encoding and HTML encoding?
URL encoding converts characters for safe transmission in web addresses using percent notation (%XX). HTML encoding converts characters for safe display in HTML using entities like & and <.
Should I encode the entire URL or just parts of it?
Encode individual components ā the path segments and query parameter values ā not the entire URL. The protocol, host, and structural characters like ? and & should remain unencoded.
Why does a space become %20 in some cases and + in others?
In percent-encoding (RFC 3986), a space is %20. In form encoding (application/x-www-form-urlencoded), a space is represented as +. Both are valid but used in different contexts.
Related guides
- What Is Base64 Encoding? How It Works and When to Use It
- What Is JSON? A Complete Guide to JavaScript Object Notation
- What Is a UUID? Understanding Universally Unique Identifiers
- What Is a Hash Function? Understanding MD5, SHA, and Cryptographic Hashing
- What Is XML? A Complete Guide to Extensible Markup Language
Last updated on 2026-09-27