URL Encoding Guide: Why and How to Encode URLs
Ever wonder why spaces in URLs become %20 and ampersands become %26? URL encoding (also called percent-encoding) converts characters that are not safe for URLs into a format that web servers can understand.
Why URL Encoding Matters
URLs can only contain a limited set of ASCII characters. Special characters, spaces, and non-ASCII text need encoding to be transmitted safely. Without encoding, URLs break in unpredictable ways β a space could truncate a URL, an ampersand could be mistaken for a parameter separator.
Try our URL Encoder/Decoder to see how any text transforms when encoded for URLs.
Which Characters Need Encoding?
Safe Characters (no encoding needed)
Letters (A-Z, a-z), digits (0-9), and these symbols: -, _, ., ~
Reserved Characters (encode when used as data)
| Character | Encoded | Purpose in URLs |
|---|---|---|
: | %3A | Scheme separator |
/ | %2F | Path separator |
? | %3F | Query string start |
# | %23 | Fragment identifier |
& | %26 | Parameter separator |
= | %3D | Key-value separator |
+ | %2B | Often represents space |
@ | %40 | User info separator |
% | %25 | Encoding indicator |
Always Encode
Spaces (%20), non-ASCII characters (UTF-8 bytes encoded), and any character outside the safe set.
URL Encoding in Practice
Query Parameters
The most common use case. When building search URLs or API requests:
https://api.example.com/search?q=hello%20world&lang=en
Form Data
HTML forms with method="GET" URL-encode field values automatically. Spaces become + in form encoding (slightly different from percent-encoding).
Path Segments
File names and resource identifiers in URL paths need encoding too. A file named my report.pdf becomes my%20report.pdf in the URL.
Encoding in Different Languages
JavaScript:
encodeURIComponent('hello world & more')
// "hello%20world%20%26%20more"
decodeURIComponent('hello%20world')
// "hello world"
Python:
from urllib.parse import quote, unquote
quote('hello world & more')
# 'hello%20world%20%26%20more'
Common Mistakes
- Double encoding β Encoding an already-encoded URL turns
%20into%2520 - Using
encodeURIinstead ofencodeURIComponentβencodeURIskips reserved characters that matter in query values - Not encoding
+signs β In URLs,+can mean space. Encode literal plus signs as%2B - Forgetting non-ASCII β Unicode characters need UTF-8 encoding first, then percent-encoding
Frequently Asked Questions
What is the difference between encodeURI and encodeURIComponent?
encodeURI encodes a complete URL, skipping characters like :, /, ?, #. encodeURIComponent encodes everything except unreserved characters β use it for individual query parameter values.
Why do spaces sometimes become + and sometimes %20?
Form encoding (application/x-www-form-urlencoded) uses + for spaces. Standard percent-encoding uses %20. Both are valid in different contexts.
Related Resources
- Base64 Encoding Explained β another essential encoding scheme
- JSON API Design Patterns β building APIs that handle encoding properly
- URL Encoder/Decoder β encode and decode URLs instantly
- Base64 Encoder β encode data in Base64 format