What is URL Encoding?
URL encoding (also called percent encoding or application/x-www-form-urlencoded) is a mechanism for converting characters into a format that can be safely transmitted over the internet. It replaces special characters with a percent sign (%) followed by two hexadecimal digits representing the character's ASCII value.
Why Do We Need URL Encoding?
- Reserved Characters: Some characters have special meanings in URLs (?, &, =, #) and must be encoded
- Unsafe Characters: Spaces and other characters aren't safe for URL transmission
- International Characters: Non-ASCII characters must be encoded for compatibility
- Data Integrity: Ensures data isn't misinterpreted by servers or browsers
- Query Parameters: Required for passing data through URL query strings
Common Reserved Characters
- ? - Marks the start of query string
- & - Separates query parameters
- = - Separates parameter name from value
- # - Indicates fragment identifier
- / - Path separator
- : - Port separator
- @ - Username/password separator
URL Encoding Rules
- Unreserved Characters: A-Z, a-z, 0-9, -, _, ., ~ (not encoded)
- Reserved Characters: Must be encoded if used outside their special context
- Other Characters: All other characters must be encoded as %XX
- Encoding Format: % followed by two hexadecimal digits
Real-World Example
Original URL: https://example.com/search?q=hello world&sort=date
Encoded URL: https://example.com/search?q=hello%20world&sort=date