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:

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:

CharacterCode pointUTF-8 bytesEncoded
ঢU+09A2E0 A6 A2%E0%A6%A2
াU+09BEE0 A6 BE%E0%A6%BE
কU+0995E0 A6 95%E0%A6%95
াU+09BEE0 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.

FunctionLeaves these ASCII characters unencoded
encodeURIComponentA–Z a–z 0–9 - _ . ~ ! ' ( ) *
encodeURIall 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.

See the encoding before you ship it. The URL Encode / Decode tool switches between component, full-URL and form (+) modes and splits any URL into its decoded query parameters. Try it now

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.

URL এনকোডিং সহজ ভাষায়: %20, + আর কখন এনকোড করবেন

একজন ইউজার আপনার সাইটে সার্চ করলেন rice & dal, অথচ ব্যাকএন্ডে পৌঁছাল শুধু rice । কেউ বাংলা শব্দসহ একটা লিংক শেয়ার করলেন, আর সেটা এসে দাঁড়াল %E0%A6%A2%E0%A6%BE…-তে। কোনো redirect URL নষ্ট হয়ে গেল %2520। তিনটি বাগের গোড়া একই: URL নিরাপদে খুব অল্প কিছু অক্ষর বহন করতে পারে, বাকি সব পার্সেন্ট-এনকোড করতে হয় — ঠিকভাবে, ঠিক একবার, আর সঠিক ফাংশন দিয়ে।

পার্সেন্ট-এনকোডিং কেন দরকার

URL-এর স্ট্যান্ডার্ড (RFC 3986) অক্ষরগুলোকে দুই ভাগে ভাগ করে:

আপনার ডেটার ভেতরে reserved অক্ষর থাকলে পার্সার বুঝতে পারে না কোনটা ডেটা আর কোনটা গঠন। ?q=rice & dal-এ & নতুন প্যারামিটার শুরু করে দেয়, ফলে q হয়ে যায় "rice "। ডেটা এনকোড করলে & হয়ে যায় %26 — তখন এটা শুধু একটা অক্ষর, বিভাজক নয়। ASCII-এর বাইরের সবকিছু — বাংলা, ইমোজি, অ্যাকসেন্টওয়ালা অক্ষর — এনকোড করতে হয়, কারণ URL সংজ্ঞায়িতই ASCII দিয়ে।

একটি অক্ষর কীভাবে %XX হয়

নিয়মটা সোজা: অক্ষরটির UTF-8 বাইটগুলো নিন, আর প্রতিটি বাইট লিখুন % আর দুই ঘরের hex দিয়ে। ASCII অক্ষর এক বাইটের, তাই স্পেস (বাইট 0x20) হয় %20, আর & (0x26) হয় %26। UTF-8-এ প্রতিটি বাংলা অক্ষর তিন বাইট নেয়। নিচে ঢাকা — এতে চারটি code point:

অক্ষরCode pointUTF-8 বাইটএনকোড করা রূপ
ঢU+09A2E0 A6 A2%E0%A6%A2
াU+09BEE0 A6 BE%E0%A6%BE
কU+0995E0 A6 95%E0%A6%95
াU+09BEE0 A6 BE%E0%A6%BE
encodeURIComponent("ঢাকা")
// "%E0%A6%A2%E0%A6%BE%E0%A6%95%E0%A6%BE"  (১২ বাইট → ৩৬টি অক্ষর)

অর্থাৎ চার অক্ষরের একটা বাংলা শব্দ আসল URL-এ হয়ে যায় ৩৬ অক্ষরের। ব্রাউজার সাধারণত অ্যাড্রেস বারে ডিকোড করা বাংলাই দেখায়, কিন্তু নেটওয়ার্কে যায় এনকোড করা রূপটা। URL-এর দৈর্ঘ্যের সীমা থাকলে এটা মাথায় রাখুন।

encodeURIComponent বনাম encodeURI

JavaScript-এ দুটি এনকোডার আছে, আর ভুলটা বেছে নেওয়াই URL-এর সবচেয়ে পরিচিত বাগ।

ফাংশনযে ASCII অক্ষরগুলো এনকোড করে না
encodeURIComponentA–Z a–z 0–9 - _ . ~ ! ' ( ) *
encodeURIওপরের সবগুলো, সাথে ; , / ? : @ & = + $ #
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"

একটি মানের ওপর কখনো encodeURI চালাবেন না। encodeURI("rice & dal") &-কে রেখে দেয়, ফলে শুরুর বাগটা থেকেই যায়। মানের জন্য সবসময় encodeURIComponent — বা আরও ভালো, কাজটা URLSearchParams-কে দিন।

স্পেস: %20 নাকি +?

দুটোই ঠিক, তবে আলাদা জায়গায়। path-এ আর সাধারণ পার্সেন্ট-এনকোডিংয়ে স্পেস হয় %20। HTML ফর্ম আলাদা একটা ফরম্যাট ব্যবহার করে — application/x-www-form-urlencoded — যেখানে query string বা ফর্ম বডিতে স্পেস হয় +, আর আসল প্লাস চিহ্ন হয় %2B। URLSearchParams এই ফর্মের নিয়ম মানে:

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"  — প্লাস স্পেসে বদলায় না

ফাঁদটা হলো: সার্ভার যদি সাধারণ percent-decoder দিয়ে query string ডিকোড করে, + প্লাসই থেকে যায়। আবার ক্লায়েন্ট যদি +8801711…-এর মতো ফোন নম্বর প্লাস এনকোড না করেই পাঠায়, form decoder সেটাকে স্পেস বানিয়ে ফেলে। দুই দিকেই মিলিয়ে একই ধরনের টুল দিয়ে এনকোড ও ডিকোড করুন।

ডাবল এনকোডিং

আগে থেকে এনকোড করা স্ট্রিং আবার এনকোড করলে প্রতিটি % হয়ে যায় %25:

encodeURIComponent(encodeURIComponent("a b"))   // "a%2520b"

সার্ভার একবার ডিকোড করে পায় a%20b, a b নয়। লগে %25 দেখা মানেই সাধারণত এই সমস্যা। এটা ঘটে যখন কোনো মান হাতে এনকোড করে এমন লাইব্রেরিতে (axios, URLSearchParams, requests) দেওয়া হয় যা নিজেই আবার এনকোড করে। সমাধান: এনকোড করুন ঠিক এক জায়গায় — আদর্শভাবে লাইব্রেরিতে, হাতে কখনো নয়।

নিরাপদে query string তৈরি করা

স্ট্রিং জোড়া লাগাবেন না। এনকোডিংয়ের কাজটা প্ল্যাটফর্মকেই করতে দিন।

// 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"

// প্যারামিটার পড়া
new URL(location.href).searchParams.get("q");   // আগেই ডিকোড করা
# 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'   — "/" ডিফল্টভাবে রেখে দেয়
quote("a b/c", safe="")  # 'a%20b%2Fc' — একটি path segment-এর জন্য এটা
parse_qs("q=a+b")        # {'q': ['a b']}

খেয়াল করুন, Python-এর quote() ডিফল্টভাবে / রেখে দেয় — পুরো path-এর জন্য ঠিক, কিন্তু ফাইলের নামের মতো একক segment-এর জন্য ভুল। একটি segment এনকোড করতে safe="" দিন।

ডিকোডিং এরর: “URIError: URI malformed”

ইনপুট বৈধ পার্সেন্ট-এনকোডিং না হলে decodeURIComponent এরর ছোড়ে:

decodeURIComponent("100%")      // URIError: URI malformed  ("%"-এর পরে দুই ঘরের hex নেই)
decodeURIComponent("%E0%A6")    // URIError: URI malformed  (অসম্পূর্ণ UTF-8 sequence)

প্রথমটা হয় যখন %-সহ কাঁচা লেখা (যেমন “50% off”) কখনো এনকোডই করা হয়নি — ঠিকভাবে এনকোড করলে হতো 50%25%20off। দ্বিতীয়টা হয় যখন লম্বা এনকোড করা স্ট্রিং কোনো অক্ষরের মাঝখানে কেটে যায়, যেমন দৈর্ঘ্যের সীমার কারণে। অবিশ্বস্ত ইনপুট ডিকোড করার সময় কলটি try…catch-এ রাখুন, আর ব্যর্থ হলে অ্যাপ ক্র্যাশ না করিয়ে সেটাকে ভুল ইনপুট হিসেবে ধরুন।

পাঠানোর আগে এনকোডিংটা নিজের চোখে দেখুন। URL এনকোড / ডিকোড টুলে component, পুরো URL আর ফর্ম (+) মোড বদলানো যায়, আর যেকোনো URL-কে ডিকোড করা query প্যারামিটারে ভেঙে দেখায়। এখনই চেষ্টা করুন

প্রশ্নোত্তর

URL আর slug-এ সরাসরি বাংলা শব্দ রাখা কি ঠিক?

কাজ করে: ব্রাউজার বাংলাই দেখায় আর এনকোড করা বাইট পাঠায়। অসুবিধা হলো দৈর্ঘ্য, আর যেসব জায়গায় কাঁচা রূপটা দেখা যায় — যেমন কিছু চ্যাট অ্যাপ ও লগ — সেখানে পড়তে কষ্ট। এজন্য অনেক সাইট প্রতিবর্ণীকৃত slug (/dhaka-weather) ব্যবহার করে।

%e0 আর %E0 কি একই?

হ্যাঁ। পার্সেন্ট-এনকোডিংয়ে hex অঙ্ক বড় হাতের বা ছোট হাতের — দুটোই চলে, তবে সামঞ্জস্যের জন্য RFC 3986 বড় হাতের অক্ষর সুপারিশ করে।

ব্রাউজার বা fetch() কি নিজেই URL এনকোড করে না?

আংশিক করে। URL পার্সার স্পেস আর non-ASCII অক্ষর এনকোড করে, কিন্তু কোন & বা = আপনার ডেটার অংশ তা সে জানে না: new URL("https://x.com/a b?q=rice & dal").href দেয় https://x.com/a%20b?q=rice%20&%20dal, অর্থাৎ query তখনো দুই ভাগ হয়ে যায়। প্রতিটি মান নিজে এনকোড করুন, অথবা searchParams ব্যবহার করুন।

URL এনকোডিং কি ডেটা লুকায় বা সুরক্ষিত রাখে?

না। যে কেউ এটা উল্টে আগের রূপে ফেরাতে পারে, কোনো গোপনীয়তা নেই। পাসওয়ার্ড বা টোকেন কখনো URL-এ রাখবেন না — URL জমা থাকে ব্রাউজার হিস্টরি, সার্ভার লগ আর Referer হেডারে।

Related guides

More free tools