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

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

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.