urlencodingweb developmenthttp
URL Encoding: Why It Matters and How It Works
Learn what URL encoding (percent encoding) is, why it exists, and how to encode/decode URLs correctly.
April 15, 2024ยท5 min read
What is URL Encoding?
URLs can only contain certain ASCII characters. Any other characters must be encoded using percent encoding: a % followed by two hex digits representing the character's byte value.
Reserved vs Unsafe Characters
Reserved characters have special meaning in URLs:
/ ? # [ ] @ ! $ & ' ( ) * + , ; =
Unsafe characters may be misinterpreted by browsers:
space < > { } | \ ^ ~ `
How Encoding Works
Space โ %20
& โ %26
= โ %3D
/ โ %2F
? โ %3F
# โ %23
In JavaScript
// Encode a full URL
encodeURI('https://example.com/path with spaces?q=hello world')
// "https://example.com/path%20with%20spaces?q=hello%20world"
// Encode a URL component (more aggressive)
encodeURIComponent('hello & goodbye')
// "hello%20%26%20goodbye"
// Decode
decodeURIComponent('hello%20world')
// "hello world"
encodeURI vs encodeURIComponent
| Function | Encodes | Leaves alone |
|---|---|---|
encodeURI |
Unsafe chars | Reserved chars (/ ? # & etc.) |
encodeURIComponent |
Unsafe + reserved | Letters, digits, - _ . ! ~ * ' ( ) |
Use our URL Encode/Decode tool for quick conversions.