URL Encoding and Decoding Explained: Complete Developer Guide
URL encoding converts special characters to a web-safe format. Learn how it works, when to use it, and avoid common mistakes in your applications.
What Is URL Encoding?
URL encoding, also known as percent encoding, is a mechanism for converting characters into a format that can be safely transmitted within a URL. Since URLs can only contain a limited set of ASCII characters, special characters must be encoded using a percent sign followed by two hexadecimal digits.
Why URL Encoding Matters
URLs have strict character limitations:
- Reserved Characters: Have special meaning in URLs
- Unsafe Characters: May cause issues in transmission
- Non-ASCII Characters: Unicode characters need encoding
- Ambiguous Characters: Could be misinterpreted
The Encoding Process
URL encoding replaces unsafe characters with a percent sign (%) followed by two hexadecimal digits representing the character's ASCII code.
Example:
- Space becomes %20
- Ampersand (&) becomes %26
- Question mark (?) becomes %3F
URL Character Categories
Unreserved Characters (Safe)
These characters can appear in URLs without encoding: A-Z a-z 0-9 - _ . ~
Reserved Characters
These have special meaning in URLs:
| Character | Meaning | Encoded |
|---|---|---|
| : | Port separator | %3A |
| / | Path separator | %2F |
| ? | Query start | %3F |
| # | Fragment start | %23 |
| @ | User info | %40 |
| & | Parameter separator | %26 |
| = | Key-value separator | %3D |
Unsafe Characters
Characters that must always be encoded:
| Character | Name | Encoded |
|---|---|---|
| Space | Space | %20 |
| " | Quote | %22 |
| < | Less than | %3C |
| > | Greater than | %3E |
URL Encoding in JavaScript
encodeURIComponent()
Encodes a complete URI component (parameter values). Use this for individual query parameter values.
encodeURI()
Encodes a complete URI (preserves URL structure characters). Use this for complete URLs.
When to Use Which
| Function | Use Case | Preserves |
|---|---|---|
| encodeURIComponent() | Individual parameters | Nothing |
| encodeURI() | Complete URLs | : / ? # @ |
URL Encoding in Other Languages
Python
Use urllib.parse.quote() and quote_plus()PHP
Use urlencode() and rawurlencode()Java
Use URLEncoder.encode()Common URL Encoding Scenarios
Query String Parameters
When building search URLs, always encode parameter values to handle special characters like spaces and ampersands.
Path Segments
User-generated content in URL paths needs encoding to handle special characters.
Form Data
Form submissions use URL encoding for the request body with content-type application/x-www-form-urlencoded.
Special Cases
Space Character
The space character has two encoding forms:
| Context | Encoding | Standard |
|---|---|---|
| Path segment | %20 | RFC 3986 |
| Query string | + | application/x-www-form-urlencoded |
Unicode Characters
Unicode characters require UTF-8 encoding first, then percent encoding of each byte.
Double Encoding Issues
A common mistake is encoding already-encoded URLs. Always encode raw values only.
URL Encoding Reference Table
Common Characters
| Character | Encoded | Description |
|---|---|---|
| (space) | %20 or + | Space character |
| ! | %21 | Exclamation |
| # | %23 | Hash |
| $ | %24 | Dollar |
| % | %25 | Percent |
| & | %26 | Ampersand |
| + | %2B | Plus |
| = | %3D | Equals |
| ? | %3F | Question |
| @ | %40 | At sign |
Common Mistakes and Solutions
Mistake 1: Encoding Entire URLs
Only encode parameter values, not the entire URL structure.Mistake 2: Not Encoding Special Characters
Special characters like & in values break URL parsing.Mistake 3: Forgetting to Decode
Always decode values when reading from URLs.Conclusion
URL encoding is essential for web development. Remember:
- Encode Parameter Values: Use encodeURIComponent for query params
- Preserve URL Structure: Use encodeURI for complete URLs
- Handle Spaces: Know when to use %20 vs +
- Avoid Double Encoding: Only encode raw values
- Unicode Support: UTF-8 encoding happens automatically
- Decode When Reading: Parse encoded values properly
Try Our Free Tools
Put these tips into practice with our free online tools. No signup required.
Explore Tools