URL Encoding Explained: Why Spaces Become %20 and How to Decode It
If you've ever seen %20 in a URL or wondered why your search query looks garbled in the address bar, you've encountered URL encoding. Here's why it exists and how it works.
Why URLs Need Encoding
URLs can only contain a limited set of ASCII characters. Characters like spaces, ampersands, question marks, and non-ASCII characters (accented letters, CJK characters, emoji) have special meaning in URL syntax or simply aren't allowed.
URL encoding (also called percent-encoding) replaces each unsafe character with a % followed by its two-digit hexadecimal value. A space is byte 0x20, so it becomes %20.
Characters That Need Encoding
The URL specification (RFC 3986) defines which characters are safe and which must be encoded:
- Safe (unreserved):
A-Z a-z 0-9 - _ . ~— never encoded - Reserved:
: / ? # [ ] @ ! $ & ' ( ) * + , ; =— encoded when used outside their reserved purpose - Everything else: Spaces, quotes, angle brackets, non-ASCII — always encoded
encodeURI vs. encodeURIComponent
JavaScript provides two encoding functions, and using the wrong one is a common source of bugs:
- encodeURI(): Encodes a complete URL. Leaves reserved characters like
/ ? # & =intact because they're part of URL structure. - encodeURIComponent(): Encodes a single URL component (a query parameter value, a path segment). Encodes everything except unreserved characters.
Rule of thumb: use encodeURIComponent for individual values, encodeURI only when encoding a full URL that you assembled from trusted parts.
The + vs. %20 Space Problem
You'll see spaces encoded two ways: %20 and +. The difference is the encoding scheme:
- %20 — RFC 3986 percent-encoding. Used in URL paths and modern applications.
- + — HTML form encoding (
application/x-www-form-urlencoded). Used in form submissions and many query strings.
Most servers accept both, but be aware of the difference when debugging encoded URLs. JavaScript's URLSearchParams uses + for spaces; encodeURIComponent uses %20.
URL Encoding in Other Languages
- Python:
urllib.parse.quote()andurllib.parse.unquote() - PHP:
urlencode()/rawurlencode()and their decode counterparts - Go:
url.QueryEscape()andurl.PathEscape() - Java:
URLEncoder.encode()— note: it uses + for spaces (form encoding)
Encode or decode any URL instantly
Paste encoded or plain text — get the decoded or encoded version. Supports full URLs and individual components.
Open URL Encoder / Decoder