What Is URL Encoding? Complete Guide to Percent-Encoding
What Is URL Encoding?
URL encoding (also called percent-encoding) converts characters into a format that can safely travel across the internet. It replaces unsafe or reserved characters with a % followed by two hexadecimal digits.
For example, a space becomes %20, and an ampersand becomes %26.
Every URL you click, every API request your app sends, and every form your users submit relies on URL encoding behind the scenes.
Why URL Encoding Matters
URLs can only carry a limited set of ASCII characters. Anything outside that set — spaces, Unicode text, or special symbols — must be encoded before it enters a URL.
Without encoding:
- Spaces break URLs — they confuse browsers and servers
- Reserved characters change meaning —
&splits query parameters,=assigns values - Unicode causes errors — non-ASCII characters like
éor你好need encoding - APIs reject bad input — unencoded payloads fail silently or return errors
Reserved Characters
RFC 3986 defines characters with special meanings in URLs. These characters must be percent-encoded when used as data, not as delimiters:
| Character | Encoded | Purpose in URLs |
|---|---|---|
! | %21 | Sub-delim |
# | %23 | Fragment identifier |
$ | %24 | Sub-delim |
& | %26 | Query parameter separator |
' | %27 | Sub-delim |
( | %28 | Sub-delim |
) | %29 | Sub-delim |
* | %2A | Sub-delim |
+ | %2B | Sub-delim |
, | %2C | Sub-delim |
/ | %2F | Path separator |
: | %3A | Scheme/port separator |
; | %3B | Sub-delim |
= | %3D | Query parameter assignment |
? | %3F | Query string start |
@ | %40 | Sub-delim |
[ | %5B | IPv6 address |
] | %5D | IPv6 address |
Unreserved Characters
These characters never need encoding:
- Letters:
A–Z,a–z - Digits:
0–9 - Hyphen:
- - Period:
. - Underscore:
_ - Tilde:
~
RFC 3986 states that unreserved characters should not be encoded. Encoding them produces equivalent but longer URLs.
%20 vs +: The Space Encoding Trap
Spaces in URLs present a historical ambiguity:
%20— true percent-encoding per RFC 3986, valid everywhere in a URL+— legacy encoding fromapplication/x-www-form-urlencoded, valid only in query strings
When to use each
- Path segments: always use
%20 - Query strings with
application/x-www-form-urlencoded:+is the standard - Query strings with
multipart/form-data: use%20 - REST API parameters: prefer
%20for safety
// JavaScript's built-in functions
encodeURIComponent('hello world') // "hello%20world"
encodeURI('hello world') // "hello%20world"
// Form encoding (application/x-www-form-urlencoded)
new URLSearchParams({ q: 'hello world' }).toString()
// "q=hello+world"
Common mistake
// WRONG: Using + in path segments
const url = `/search/hello+world` // Server reads "hello+world" literally
// CORRECT: Use %20 in path segments
const url = `/search/hello%20world` // Server reads "hello world"
encodeURIComponent vs encodeURI
JavaScript provides two encoding functions with different scopes:
| Function | Encodes | Leaves unencoded |
|---|---|---|
encodeURI | Spaces, non-ASCII, #, ?, &, = | :/?#[]@!$&'()*+,;= (URL structure chars) |
encodeURIComponent | Spaces, non-ASCII, all reserved chars | -_.!~*'() only |
Practical guidelines
- Use
encodeURIwhen encoding a complete URL - Use
encodeURIComponentwhen encoding a single query parameter value - Never use
encodeURIon query values — it leaves&and=unencoded
// Encoding parts of a query string
const query = `q=${encodeURIComponent('JS & React')}&lang=${encodeURIComponent('en')}`
// "q=JS%20%26%20React&lang=en"
Common Encoding Examples
| Raw Value | Encoded | Context |
|---|---|---|
hello world | hello%20world | Space in query |
price=10&sale=5 | price%3D10%26sale%3D5 | Ampersand/equal as data |
/api/users | %2Fapi%2Fusers | Slash as data value |
café | caf%C3%A9 | Unicode character |
hello@example | hello%40example | At sign as data |
Browser Behavior
Modern browsers handle URL encoding automatically in several contexts:
- Address bar: Browsers display decoded URLs but send encoded URLs
fetch()andXMLHttpRequest: Do not auto-encode your URL — you must encode it<a href>: Browsers auto-encode paths but not query strings consistently- Form submission: Uses
application/x-www-form-urlencodedrules (spaces →+)
What you need to handle manually
- Building API request URLs in JavaScript
- Constructing URLs with dynamic path segments
- Passing special characters as query parameter values
- Sharing URLs that contain non-ASCII text
5 Best Practices
- Always encode query parameter values with
encodeURIComponent - Don't double-encode — check if data is already encoded before encoding again
- Use
%20for paths,+only for form-encoded query strings - Decode server-side before processing — never trust raw URL input
- Validate decoded values — URL encoding is not sanitization
Related Guides
Try It Yourself
Encode and decode URLs instantly with our free URL Encoder/Decoder tool. Paste any text or URL and get the correctly encoded result in one click.