URL Encoding and Decoding: Character Encoding Guide
Learn URL encoding (percent encoding) and decoding operations. Special characters, UTF-8 encoding, and implementation in different programming languages.
URL encoding (percent encoding) is a mechanism used to represent unsafe or special characters in URLs. In this guide, we'll examine URL encoding and decoding operations, common use cases, and implementations in different programming languages.
What is URL Encoding?
URL encoding is a method of representing characters not in the ASCII character set or having special meaning in URLs using a percent sign (%) and two hexadecimal digits.
Basic Rules
- Space → %20 or +
- Safe characters not encoded: A-Z, a-z, 0-9, -, _, ., ~
- Reserved characters must be encoded: !, #, $, &, etc.
Common Character Encodings
Space=%20, #=%23, &=%26, /=%2F, ?=%3F, @=%40
JavaScript URL Encoding
// encodeURIComponent
const encoded = encodeURIComponent("Hello World");
// Hello%20World
// decodeURIComponent
const decoded = decodeURIComponent(encoded);PHP URL Encoding
$encoded = urlencode("Hello World");
$decoded = urldecode($encoded);Python URL Encoding
from urllib.parse import quote, unquote
encoded = quote("Hello World")
decoded = unquote(encoded)URL Encoding and SEO
- Prefer ASCII characters for slugs
- Keep URLs readable
- Use canonical for encoded/decoded versions
Conclusion
URL encoding is fundamental in web development. For SEO, using slugs instead of encoded URLs is better for both user experience and search engine optimization.