In a URL, a space is always safe as %20. The + form only means a space inside query strings and form bodies sent as application/x-www-form-urlencoded; in a path it is a literal plus sign. Letters, digits and - . _ ~ never need escaping. Everything else should be percent-encoded when it is data rather than a delimiter.
This guide explains where each rule comes from, which JavaScript function to use for which part of a URL, and the bugs that come from mixing the two space conventions. Every encoded string below was produced by the encoding modes of AltFTool's URL encoder and decoder, which offers component, strict RFC 3986, whole-URL and form-body modes when you choose Escape / unescape in its Mode picker.
Two standards, two ways to write a space
The URI standard, RFC 3986, has one escaping mechanism: a percent sign followed by two hexadecimal digits for each byte, so a space (byte 0x20) is %20. It says nothing about + meaning a space.
The plus sign comes from HTML forms. The W3C's HTML 4.01 specification defines the default form encoding, application/x-www-form-urlencoded, as: space characters are replaced by +, then other non-alphanumeric characters are replaced by %HH. That is why search URLs produced by forms are full of plus signs. MDN's page on URLSearchParams notes that it, too, interprets + as a space.
| Where the text goes | Space becomes | A real plus sign becomes |
|---|---|---|
Path segment (/files/my report.pdf) | %20 | + (literal) or %2B |
| Query value, built by hand | %20 (always safe) | %2B |
| HTML form body or form-style query | + | %2B |
Fragment after # | %20 | + (literal) |
The safe habit is to write spaces as %20 everywhere and to always encode a real plus sign as %2B. A form-aware decoder will read both correctly, and nothing will mistake your plus sign for a space.
Which characters need escaping
RFC 3986 sorts ASCII characters into three groups. Unreserved characters (A–Z, a–z, 0–9, hyphen, period, underscore and tilde) never need encoding. Reserved characters have a job as delimiters: the general delimiters : / ? # [ ] @ and the sub-delimiters ! $ & ' ( ) * + , ; =. Everything else, including spaces, quotes, % itself and every non-ASCII character, must be percent-encoded.
Reserved characters are only encoded when they are data. The / between path segments and the & between query parameters must stay as they are; a / or & inside a search term must be escaped, or the server will split the value in the wrong place. Non-ASCII text is converted to UTF-8 bytes first and each byte is escaped, which is why the output grows so quickly.
| Input | Component mode output | Growth |
|---|---|---|
a b | a%20b | 66.7% |
50% off | 50%25%20off | 57.1% |
C++ & C# | C%2B%2B%20%26%20C%23 | 150% |
café | caf%C3%A9 | 125% |
₹500 | %E2%82%B9500 | 200% |
😀 | %F0%9F%98%80 | 500% |
~user_name-1.0 | ~user_name-1.0 | 0% |
Encode or decode text, inspect query parameters, or pick Escape / unescape for strict RFC 3986 and form-body modes.
encodeURIComponent, encodeURI and strict mode
JavaScript has two built-in encoders, and choosing the wrong one is the most common mistake.
encodeURIComponent() is for one piece of a URL, such as a single query value or path segment. It escapes the delimiters, so /path?x=1 becomes %2Fpath%3Fx%3D1. encodeURI() is for a whole URL that is already structured, so it leaves : / ? # & = alone; fed C++ & C#, it returns C++%20&%20C#, which a server would read as a query split at & with plus signs treated as spaces, and the browser would treat everything after # as a fragment that never reaches the server.
Neither function follows RFC 3986 exactly. MDN's reference lists the characters encodeURIComponent() leaves unescaped, which include ! ' ( ) *, and notes that RFC 3986 reserves those five. For OAuth signatures, AWS-style canonical requests and other places where both sides must produce byte-identical strings, use a strict encoder. The tool's strict mode turns it's (fine)!* into it%27s%20%28fine%29%21%2A, while component mode leaves it as it's%20(fine)!*.
| Mode | C++ & C# becomes | Use it for |
|---|---|---|
| Component | C%2B%2B%20%26%20C%23 | One query value or path segment |
| Strict RFC 3986 | C%2B%2B%20%26%20C%23 | Signatures and exact comparisons |
| Whole URL | C++%20&%20C# | An already-structured URL |
| Form body | C%2B%2B+%26+C%23 | x-www-form-urlencoded bodies |
Decoding bugs: the vanishing plus and double encoding
Decoding must use the same convention as encoding. Take the form-encoded string C%2B%2B+%26+C%23. A form decoder turns + into spaces and returns C++ & C#. A plain decodeURIComponent() leaves the plus signs alone and returns C+++&+C#. The reverse mistake is worse: decoding a phone number such as +44 with form rules silently turns it into 44 with a leading space.
Double encoding happens when text is encoded twice: café becomes caf%C3%A9, then caf%25C3%25A9 because the % itself is escaped to %25. One decode then yields caf%C3%A9 rather than café. The decoder flags any input containing %25 followed by two hex digits as likely double-encoded. It also reports a broken escape, such as a bare 100%, by position, because a literal percent sign must be written %25.
When a URL comes from somewhere you do not control, inspect it before you trust it. The decoder's query view splits ?q=C%2B%2B+tips&tag=a+b into q = C++ tips and tag = a b, which shows exactly what the server will receive. If the URL is a short link, the URL redirect checker shows where it finally leads.
One related case: Base64 values in URLs. Standard Base64 contains +, / and =, all of which would need escaping. The URL-safe Base64 alphabet replaces them, which the Base64 URL encoder produces directly.
Frequently asked questions
Either works in a query string that the server parses as form data, but %20 is correct in every part of a URL. Use + only when you are producing an application/x-www-form-urlencoded body or mimicking a browser form.
An unescaped # starts the fragment, which the browser keeps to itself and never sends to the server. Encode a # inside a value as %23, as component mode does for C#.
Not when they separate path segments. Encode a slash as %2F only when it is part of a value, such as a search term or a file name inside one path segment.
Both decode the same way. RFC 3986 says uppercase and lowercase hex digits are equivalent and that producers and normalisers should use uppercase, so %2B rather than %2b.
MDN documents that it throws when the string contains a lone surrogate, half of a character such as an emoji that was cut in the middle. Such text has no valid UTF-8 form, so it cannot be percent-encoded.
Sources and notes
Links the author listed for this post.
- 1
- 2
- 3encodeURIComponent() (opens in a new tab)
MDN Web Docs
- 4encodeURI() (opens in a new tab)
MDN Web Docs
- 5URLSearchParams (opens in a new tab)
MDN Web Docs







Discussion
0 comments