When URLs Need Encoding (and When They Don't)
You paste a link into a chat. It looks fine on your screen. Then a friend taps it and lands on a 404, or on a search page with half the words missing. The link broke somewhere between your clipboard and their browser, and the cause was a space. One small space in the middle of the address cut the link in half.
I have watched the same thing happen with Arabic links more times than I can count. A shop sends a customer a product link with an Arabic search term in it. The customer taps it, the app opens the link up to the first odd character, and the rest is gone. The shop owner assumes the site is broken. The site is fine. The link was never safe to share in its raw form. Paste it into our URL Encoder / Decoder first and the problem goes away in seconds.
Quick answer: Encode a URL when it holds spaces, non-English letters, emoji, or a character like & or # inside a value. Each unsafe character becomes a percent sign followed by two hex digits, so a space turns into %20. Leave plain addresses alone. Letters, digits, hyphens and slashes need no encoding, and encoding a link a second time breaks it.
What percent-encoding actually is
A URL was built for a small set of characters. Letters, digits and a few marks like the hyphen and the slash travel fine. Anything outside that set has to be written in a code the browser can carry: a percent sign followed by two hexadecimal digits. A space is %20. An ampersand is %26. A question mark is %3F. The percent sign itself is %25, which is why an already-encoded link can go wrong so fast if you encode it twice.
Letters from other alphabets take a longer route. Arabic, Chinese, Cyrillic and emoji are first turned into UTF-8 bytes, and each byte becomes its own percent code. The Arabic word السلام turns into %D8%A7%D9%84%D8%B3%D9%84%D8%A7%D9%85. That looks ugly in an address bar, and it is the reason some people avoid non-English text in links entirely. They do not need to. The encoded form works exactly the same as the readable one, and every modern browser decodes it back when it shows the page.
You never have to work out the codes by hand. A tool does it in one step. And most of the addresses you share every day need no encoding at all, because they are already made of safe characters. The skill is knowing which links fall into which group.
Encode to send, decode to read
You encode a link before you share it or store it. You decode one after you receive it, when you want to see what it really says. The two directions solve different problems, and mixing them up is where most of the confusion starts.
Say a customer emails you a link that looks like this: https://example.com/search?q=%D8%A7%D9%84%D8%B3%D9%84%D8%A7%D9%85. You cannot tell what they searched for, and you cannot paste it into a report as it stands. Run it through the decoder and it becomes https://example.com/search?q=السلام. Same link, readable at last.
The reverse trip matters just as much. You write a search box on your site, a visitor types two Arabic words, and your code builds a link from what they typed. If the code drops the raw words into the address, the link may work in your browser and fail in theirs. Encode the value first and the link survives the trip. The rule is short: encode on the way out, decode on the way in.
encodeURI vs encodeURIComponent: pick the right one
JavaScript gives you two functions that sound almost the same, and using the wrong one is the fastest way to break a link that looked fine.
encodeURI is for a whole address. It leaves the parts that hold a URL together alone: the ://, the slashes, the question mark, the ampersand between parameters, the hash. It only fixes the unsafe characters, so a space inside a search term becomes %20 while the rest of the address keeps working.
encodeURIComponent is for one value inside an address. It encodes everything except letters, digits and a few safe marks, including the ? and the & and the =. That is what you want for a search term, because an & inside a search term would otherwise split your value into two parameters.
| Function | Leaves alone | Use it for |
|---|---|---|
encodeURI | ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) # | A whole URL |
encodeURIComponent | A-Z a-z 0-9 - _ . ! ~ * ' ( ) | One value inside a URL |
A quick test tells you which one you used. Encode a whole URL with encodeURIComponent and the :// becomes %3A%2F%2F, which no browser can open as a link. Encode a search term with encodeURI and an & inside the term stays put, which splits the value in two. If your tool has a mode switch, use it. Ours does.
When you need to encode
You need encoding whenever a link carries something outside the safe set. That happens more often than most people expect.
- A query string with a search term. Any value after ?q= or &lang= can hold spaces, punctuation or words in another language.
- A file name with spaces. A PDF called "price list 2026.pdf" needs its spaces encoded before it works as a link.
- Text in another alphabet. Arabic, Urdu, Chinese and other scripts all need encoding in the address itself, even though the browser displays them decoded.
- Links pasted into email or chat. Some apps cut a link off at the first space or bracket, so encoding first keeps the whole address in one piece.
- Values you build in code. If your script joins user input into a URL, encode each value before you join it.
When you can leave a URL alone
Not every link needs this. A plain address made of lowercase letters, digits, hyphens and slashes is already safe. Your slugs are a good example. If you write slugs the way our guide to SEO-friendly URL slugs describes, with short lowercase words joined by hyphens, nothing in them needs encoding. That is one more reason to keep slugs clean: they survive copying, pasting and sharing without any extra work.
The same goes for a link that is already encoded. If an address already holds %20 and %D8 codes, leave it alone. Encoding it again turns every % into %25, and %20 becomes %2520. The server then looks for a page with a literal %20 in its name, finds nothing, and returns a 404.
Links your platform builds for you are usually handled. WordPress, Shopify and most site builders encode query values when they build a link, so step in only when you write or paste one by hand.
Three mistakes that break links
First, double encoding. This is the one I see most. A link arrives already encoded, someone runs it through an encoder again, and %20 becomes %2520. The fix is to decode once before you do anything else.
Second, encoding a whole URL with encodeURIComponent. It feels like the safer choice because it encodes more, but it encodes the :// and the ?, so the address stops being an address. Use encodeURI for a whole URL and save encodeURIComponent for the values inside it.
Third, the plus sign. In a query string, a plain + is read as a space by many servers. That is fine when your value really holds a space, and it is a real problem when your value holds a plus sign, like a phone number or a C++ search. A real plus has to be written as %2B. If your searches for C++ keep returning results for C, this is probably why.
A worked example: an Arabic search term
Say you run a small shop and a customer searches your site for "حذاء رياضي", running shoes in Arabic. The address your site builds looks like this before encoding: https://example.com/search?q=حذاء رياضي
The space between the two words is the first problem, and the Arabic letters are the second. Encode the value with encodeURIComponent and you get: https://example.com/search?q=%D8%AD%D8%B0%D8%A7%D8%A1%20%D8%B1%D9%8A%D8%A7%D8%B6%D9%8A
That long string looks worse, and it works better. It survives email, chat apps and copy-paste without losing a character, and the server reads it back as the exact search the customer typed. Decode it on your side and you see the original words again.
Frequently asked questions
Do I need to encode every URL I share?
No. Plain addresses made of letters, digits, hyphens and slashes are already safe. Encode only when a link holds spaces, non-English letters, emoji, or a character like & or # inside a value.
Why does my link show %20 instead of spaces?
Because the spaces were percent-encoded so the link survives sharing. A space becomes %20, and the browser shows the readable version again once the page loads.
What happens if I encode a URL twice?
Every percent sign becomes %25, so %20 turns into %2520 and the server looks for a page with a literal %20 in its name. Decode once to get the original back.
Should slugs be encoded?
No. A good slug only holds safe characters: lowercase letters, digits and hyphens. That is one more reason to keep slugs clean, so they need no encoding at all. See our guide to writing SEO-friendly URL slugs.
Does encoding help SEO?
Encoding does not raise rankings by itself. It keeps links working, and working links keep their clicks and their crawl value. A link that returns a 404 helps no one. For the bigger picture on keeping your pages clean, see our guide to canonical URLs and the on-page SEO checklist.
Related guides
- How to Write SEO-Friendly URL Slugs
- What Is a Canonical URL and How to Set One
- On-Page SEO Checklist for Beginners
Next time a link looks wrong, do not rewrite the page behind it. Paste the address into our URL Encoder / Decoder, check whether it needs encoding or decoding, and fix the address itself. It takes ten seconds, and it is the cheapest link repair you will ever do.