URL Encoding Explained: How to Encode and Decode URLs (With Examples)
Updated November 5, 2026 · 8 min read
Ever seen a link full of %20, %3A and %2F and wondered what
happened to it? That's URL encoding (also called percent-encoding). It's how URLs
safely carry spaces, symbols and non-English characters. Understanding it takes five minutes and
saves hours of debugging broken links, search forms and API calls.
Why URLs need encoding
URLs may only contain a limited set of characters, and some characters have special jobs:
/separates path segments.?starts the query string.&separates parameters, and=joins a name to its value.#starts the fragment (the part after the page address).
So if a search term contains &, as in salt & pepper, the browser would
think a new parameter starts there. Encoding turns it into %26, which means "a literal
ampersand, not a separator".
How percent-encoding works
Each unsafe character is replaced with % followed by its byte value in hexadecimal.
Characters outside basic English, such as é or 日, are first converted to
UTF-8 bytes, and each byte is encoded: é becomes %C3%A9.
Common codes
| Character | Encoded | Character | Encoded |
|---|---|---|---|
| space | %20 (or + in forms) | & | %26 |
/ | %2F | = | %3D |
? | %3F | # | %23 |
: | %3A | + | %2B |
@ | %40 | % | %25 |
" | %22 | é | %C3%A9 |
Letters, digits and - _ . ~ never need encoding.
Encode a value, or a full URL?
This is the most common source of bugs:
- Encoding a single value (a search term, a redirect target, an email address)
converts every special character, including
/,:,?and&. Use this when you're putting one piece of data inside a URL. - Encoding a full URL leaves the structural characters alone so the link still works, and only encodes things like spaces and non-English letters.
Value: https://example.com/a b?x=1
Encoded as a value: https%3A%2F%2Fexample.com%2Fa%20b%3Fx%3D1
Encoded as a URL: https://example.com/a%20b?x=1
The first form is what you need when passing a URL as a parameter, for example
?redirect=https%3A%2F%2Fexample.com%2Fdone.
Examples in code
JavaScript
encodeURIComponent('salt & pepper') // "salt%20%26%20pepper" (a value)
encodeURI('https://example.com/a b') // "https://example.com/a%20b" (a whole URL)
decodeURIComponent('salt%20%26%20pepper') // "salt & pepper"
// Easiest for query strings: let the browser do it
const params = new URLSearchParams({ q: 'salt & pepper', page: 2 });
params.toString() // "q=salt+%26+pepper&page=2"
PHP
rawurlencode('salt & pepper'); // "salt%20%26%20pepper"
urlencode('salt & pepper'); // "salt+%26+pepper" (form style)
http_build_query(['q' => 'salt & pepper', 'page' => 2]);
Python
from urllib.parse import quote, urlencode
quote('salt & pepper') # 'salt%20%26%20pepper'
urlencode({'q': 'salt & pepper', 'page': 2}) # 'q=salt+%26+pepper&page=2'
Fixing common problems
| Symptom | Cause | Fix |
|---|---|---|
%2520 in a URL | Encoded twice (% became %25) | Encode once, at the point where you build the URL |
Parameters cut off at & | A value wasn't encoded | Encode each value with encodeURIComponent |
+ turns into a space | Form-style decoding treats + as space | Encode real plus signs as %2B |
Strange characters like é | UTF-8 decoded as another character set | Use UTF-8 everywhere |
Encode or decode online
To quickly read a messy link or prepare a value, paste it into the URL encoder and decoder. Choose whether you're encoding a single value or a full URL, and it shows the result instantly in your browser. To inspect each part of a link (protocol, host, path, parameters), use the URL parser.
Encode a single value or a full URL, or decode a messy link back to readable text. Runs in your browser.
Frequently asked questions
What is URL encoding?
Replacing characters that are not allowed or have special meaning in a URL with a percent sign and two hexadecimal digits. A space becomes %20, an ampersand becomes %26.
Is a space %20 or +?
In the path of a URL, a space is %20. In form data and query strings sent as application/x-www-form-urlencoded, a space is often written as +. Most decoders handle both in query strings.
What is the difference between encoding a value and encoding a full URL?
Encoding a single value (like a search term) converts every special character, including / ? & and :. Encoding a full URL leaves the characters that give the URL its structure alone, so the link still works.
What does %2520 mean?
It is a double-encoded space: %20 was encoded again, turning % into %25. Decode it twice, and fix the code that encodes it twice.
Which JavaScript function should I use?
encodeURIComponent() for individual values such as query parameters, and encodeURI() for a whole URL that is already structured.
Related articles
How to Create an SEO-Friendly URL Slug: Rules, Examples and Mistakes
What a URL slug is, 9 rules for SEO-friendly slugs with good and bad examples, how to handle accents and stop words, and how to change a slug safely with a 301 redirect.
Read More →How to Format, Validate and Fix JSON (With Common Errors Explained)
Turn minified JSON into readable text, validate it, and fix the most common JSON errors: trailing commas, single quotes, unquoted keys and comments. Plus formatting in VS Code and on the command line.
Read More →What Happens When You Paste a Long URL into a Free Shortener
A behind-the-scenes look at URL shortening: how the short code is made, what happens when someone clicks, and what to check before you share.
Read More →