If you have ever seen %20 in a link or had a URL break because of an & in a search term, you have met URL encoding. It is simple once you know the rules, and getting it wrong causes some of the most confusing bugs on the web. This guide covers the rules and the common mistakes. To encode or decode something right now, use the free URL Encode / Decode tool.
Why URLs need encoding
A URL can only contain a limited set of characters, and some of those have special jobs:
| Character | Job in a URL |
|---|---|
/ | Separates path segments |
? | Starts the query string |
& | Separates query parameters |
= | Separates a parameter name from its value |
# | Starts the fragment |
: | Separates the scheme (https) and port |
So if a search term contains & — say “chai & samosa” — and you put it in a URL unencoded, ?q=chai & samosa is read as a parameter q=chai plus a second, broken parameter. Encoding turns the special characters into harmless codes.
How percent-encoding works
Each character that is not allowed is replaced by % followed by two hexadecimal digits — the value of the byte. A space is byte 32, which is 20 in hex, so it becomes %20.
Characters outside basic English are first converted to UTF-8 bytes, then each byte is encoded:
| Character | UTF-8 bytes | Encoded |
|---|---|---|
| space | 20 | %20 |
| & | 26 | %26 |
| é | C3 A9 | %C3%A9 |
| ₹ | E2 82 B9 | %E2%82%B9 |
| न (Hindi “na”) | E0 A4 A8 | %E0%A4%A8 |
The characters that never need encoding are the unreserved ones: letters A–Z and a–z, digits 0–9, and - . _ ~.
%20 or +?
Both are used for a space, in different places:
- In a URL path and in modern encoding functions, a space is
%20. - In HTML form submissions (
application/x-www-form-urlencoded), a space is+, and a real plus sign is sent as%2B.
Most servers accept both in a query string, which is why the mix-up often goes unnoticed — until a phone number like “+91 98765 43210” arrives as “ 91 98765 43210” because a + was read as a space. When decoding form data, convert + to a space first; when sending a plus sign, make sure it is encoded as %2B.
encodeURI vs encodeURIComponent
JavaScript has two functions, and choosing the wrong one is the most common mistake:
- encodeURIComponent is for one piece of a URL — a single query value, a path segment, a URL passed as a parameter. It encodes
& = ? / # :and more, so the piece cannot break the URL. - encodeURI is for a whole URL. It leaves
: / ? # & =alone so the URL keeps its structure, and encodes only characters that are never allowed, such as spaces and non-English letters.
const q = 'chai & samosa';
'https://example.com/search?q=' + encodeURIComponent(q);
// https://example.com/search?q=chai%20%26%20samosa
encodeURI('https://example.com/my page?q=café');
// https://example.com/my%20page?q=caf%C3%A9The URL Encode / Decode tool offers both as Component and Full URL modes and gives the same output as these functions.
Better still, let the language build the query for you:
const params = new URLSearchParams({ q: 'chai & samosa', page: 2 });Note that URLSearchParams follows the form rules and writes spaces as +.
In Python and PHP
from urllib.parse import quote, quote_plus, urlencode
quote('chai & samosa', safe='') # chai%20%26%20samosa
quote_plus('chai & samosa') # chai+%26+samosa
urlencode({'q': 'chai & samosa'}) # q=chai+%26+samosarawurlencode('chai & samosa'); // chai%20%26%20samosa
urlencode('chai & samosa'); // chai+%26+samosa
http_build_query(['q' => 'chai & samosa']);Double encoding
If you see %2520 in a URL, the text was encoded twice: the first pass turned a space into %20, and the second turned the % into %25. The server then receives the literal text “%20” instead of a space. It usually happens when code encodes a value and a library encodes it again. Encode once, at the point where you build the URL. To repair a double-encoded value, decode it twice.
Practical examples
- WhatsApp click-to-chat:
https://wa.me/919876543210?text=followed by the message encoded as a component. - Redirect after login:
/login?next=followed by the full return URL encoded as a component, so its own?and&do not leak into the outer URL. - Campaign tracking: UTM values with spaces or
&must be encoded; the UTM Builder does this for you.
Not the same as Base64 or HTML entities
- URL encoding makes text safe inside a URL.
- Base64 turns any data, including binary, into text — see Base64 Encode / Decode.
- HTML entities like
&make characters safe inside HTML — see our guide to HTML entities.



