URL encoding explained: %20, + and when to encode
A user searches your site for rice & dal, and your backend receives only rice . Someone shares a link with a Bangla word in it and it arrives as %E0%A6%A2%E0%A6%BE…. A redirect URL gets mangled into %2520. All three bugs have the same root: a URL can only carry a small set of characters safely, and everything else has to be percent-encoded — correctly, exactly once, with the right function.
Why percent-encoding exists
The URL standard (RFC 3986) splits characters into two groups:
- Unreserved —
A–Z a–z 0–9 - . _ ~. These never need encoding and mean the same thing everywhere. - Reserved — characters with a job in URL syntax:
: / ? # [ ] @(they separate scheme, host, path, query and fragment) and! $ & ' ( ) * + , ; =(used inside components, for example&and=in a query string).
If your data contains a reserved character, the parser can’t tell data from syntax. In ?q=rice & dal the & starts a new parameter, so q becomes "rice ". Encoding the data turns & into %26, which is just a character, not a separator. Anything outside ASCII — Bangla, emoji, accented letters — also has to be encoded, because URLs are defined over ASCII.
How a character becomes %XX
The rule is simple: take the character’s UTF-8 bytes and write each byte as % plus two hex digits. ASCII characters are one byte, so a space (byte 0x20) becomes %20 and & (0x26) becomes %26. Bangla letters take three bytes each in UTF-8. Here is ঢাকা, which is four code points:
| Character | Code point | UTF-8 bytes | Encoded |
|---|---|---|---|
| ঢ | U+09A2 | E0 A6 A2 | %E0%A6%A2 |
| া | U+09BE | E0 A6 BE | %E0%A6%BE |
| ক | U+0995 | E0 A6 95 | %E0%A6%95 |
| া | U+09BE | E0 A6 BE | %E0%A6%BE |
encodeURIComponent("ঢাকা")
// "%E0%A6%A2%E0%A6%BE%E0%A6%95%E0%A6%BE" (12 bytes → 36 characters)
So a four-letter Bangla word becomes 36 characters in the raw URL. Browsers usually display the decoded Bangla in the address bar, but what goes over the network is the encoded form. Keep that in mind if you have URL length limits.
encodeURIComponent vs encodeURI
JavaScript has two encoders, and picking the wrong one is the most common URL bug.
encodeURIComponentis for a piece of a URL: one query value, one path segment. It encodes every reserved character, so the value can’t break out of its slot.encodeURIis for a whole URL that is already correctly structured. It leaves the syntax characters alone and only encodes spaces, non-ASCII and a few unsafe characters.
| Function | Leaves these ASCII characters unencoded |
|---|---|
encodeURIComponent | A–Z a–z 0–9 - _ . ~ ! ' ( ) * |
encodeURI | all of the above, plus ; , / ? : @ & = + $ # |
encodeURIComponent("a b&c=d/e?f#g") // "a%20b%26c%3Dd%2Fe%3Ff%23g"
encodeURI("https://x.com/a b?q=ঢাকা&x=1#top")
// "https://x.com/a%20b?q=%E0%A6%A2%E0%A6%BE%E0%A6%95%E0%A6%BE&x=1#top"
Never use encodeURI on a single value. encodeURI("rice & dal") keeps the &, so the bug from the introduction survives. For values, always use encodeURIComponent — or better, let URLSearchParams do it.
Space: %20 or +?
Both are correct, in different places. In a path and in general percent-encoding, a space is %20. HTML forms use a separate format, application/x-www-form-urlencoded, where a space in the query string or form body becomes + and a literal plus becomes %2B. URLSearchParams follows the form rules:
new URLSearchParams({ q: "a b+c" }).toString() // "q=a+b%2Bc"
new URLSearchParams("q=a+b%20c").get("q") // "a b c"
decodeURIComponent("a+b") // "a+b" — plus is NOT turned into a space
The trap: if a server decodes a query string with a plain percent-decoder, + stays a plus. If a client encodes a phone number like +8801711… without encoding the plus, a form decoder turns it into a space. Encode and decode with matching tools on both sides.
Double encoding
If you encode a string that is already encoded, every % becomes %25:
encodeURIComponent(encodeURIComponent("a b")) // "a%2520b"
The server decodes once and gets a%20b instead of a b. Seeing %25 in logs is the classic sign. It usually happens when a value is encoded manually and then passed to a library (axios, URLSearchParams, requests) that encodes it again. The fix is to encode in exactly one place — ideally the library, and never by hand.
Building query strings safely
Don’t concatenate strings. Let the platform encode for you.
// JavaScript (browser and Node.js)
const url = new URL("https://example.com/search");
url.searchParams.set("q", "rice & dal");
url.searchParams.set("page", "2");
url.href // "https://example.com/search?q=rice+%26+dal&page=2"
// Reading parameters
new URL(location.href).searchParams.get("q"); // already decoded
# Python
from urllib.parse import urlencode, quote, parse_qs
urlencode({"q": "rice & dal", "city": "ঢাকা"})
# 'q=rice+%26+dal&city=%E0%A6%A2%E0%A6%BE%E0%A6%95%E0%A6%BE'
quote("a b/c") # 'a%20b/c' — "/" is kept by default
quote("a b/c", safe="") # 'a%20b%2Fc' — use this for one path segment
parse_qs("q=a+b") # {'q': ['a b']}
Note Python’s quote() keeps / by default, which is right for a full path but wrong for a single segment such as a file name. Pass safe="" when encoding one segment.
Decoding errors: “URIError: URI malformed”
decodeURIComponent throws when the input isn’t valid percent-encoding:
decodeURIComponent("100%") // URIError: URI malformed ("%" not followed by two hex digits)
decodeURIComponent("%E0%A6") // URIError: URI malformed (incomplete UTF-8 sequence)
The first happens when raw text with a % (like “50% off”) was never encoded — correctly encoded it would be 50%25%20off. The second happens when a long encoded string is cut in the middle of a character, for example by a length limit. When decoding untrusted input, wrap the call in try…catch and treat failure as bad input rather than crashing.
FAQ
Should I put Bangla words directly in URLs and slugs?
It works: browsers show the Bangla and send the encoded bytes. The downside is length and readability when the link is copied into places that show the raw form, such as some chat apps and logs. Many sites use a transliterated slug (/dhaka-weather) for that reason.
Is %e0 the same as %E0?
Yes. Hex digits in percent-encoding are case-insensitive, though RFC 3986 recommends uppercase for consistency.
Doesn’t the browser or fetch() encode the URL for me?
Only partly. The URL parser encodes spaces and non-ASCII characters, but it can’t know which & or = belong to your data: new URL("https://x.com/a b?q=rice & dal").href gives https://x.com/a%20b?q=rice%20&%20dal, so the query is still split in two. Encode each value yourself, or use searchParams.
Does URL encoding hide or protect data?
No. It is reversible by anyone and offers no secrecy. Never put passwords or tokens in a URL — URLs end up in browser history, server logs and Referer headers.