There are two ways to write a space in a URL, %20 and +, and they are not interchangeable. Using the wrong one produces bugs that look like data corruption: a name with a space in it gains a plus sign, or an email address loses one.
The short rule is that %20 is always safe, and + means a space only in one specific place.
Two encodings that look alike
Percent-encoding is the general mechanism in the URL standard, RFC 3986. Any byte can be written as % and two hex digits, and a space is %20. In this scheme + is an ordinary character: it is never a space.
Form encoding, application/x-www-form-urlencoded, is what HTML forms use to build a query string. It is a different format, defined in the WHATWG URL standard, and in it a space is written as +. Because there is a rule that + means a space, a real plus sign must be written %2B.
So the same character means different things depending on where it appears:
| Where | %20 |
+ |
|---|---|---|
Path (/a%20b) |
A space | A literal plus |
| Query string, read as a form | A space | A space |
| Query string, read as plain percent-encoding | A space | A literal plus |
The third row is the problem. A query string is not inherently form-encoded. It becomes so only if the code reading it decides to treat it that way.
What JavaScript does
Run these and the difference is plain:
encodeURIComponent("a b+c&d") // "a%20b%2Bc%26d"
new URLSearchParams({ q: "a b+c&d" }).toString() // "q=a+b%2Bc%26d"
decodeURIComponent("a+b") // "a+b" the plus stays
new URLSearchParams("q=a+b").get("q") // "a b" the plus becomes a space
encodeURIComponent writes %20. URLSearchParams writes +. And the two decoders disagree about what a plus means. A value encoded by one and decoded by the other is where bugs come from.
Re-serialising changes the string even when it does not change the meaning: new URLSearchParams("q=a%20b").toString() returns q=a+b. That is harmless for ordinary links, but anything that compares URLs as strings, such as a signed URL or a cache key, can fail once a library has quietly swapped %20 for +.
The URL object, for what it is worth, writes %20 for a space typed into the query: new URL("https://x.test/?q=a b").search is ?q=a%20b.
Which function gives which
The same split exists in every language, so it helps to know which encoder you are actually calling:
| Language | Writes %20 (percent-encoding) |
Writes + (form encoding) |
|---|---|---|
| JavaScript | encodeURIComponent |
URLSearchParams |
| Python | urllib.parse.quote |
quote_plus, and urlencode by default |
| PHP | rawurlencode |
urlencode |
| Java | none built in | URLEncoder.encode |
Java's URLEncoder is a form encoder, which is why code that builds paths with it often replaces + with %20 afterwards. Most server frameworks parse the query string as form data, so they read + as a space there, while the path is decoded separately and keeps + as a plus.
Non-English characters
Characters outside ASCII are converted to UTF-8 bytes first, and then each byte is percent-encoded. So é becomes %C3%A9, and an emoji, which takes four bytes, becomes %F0%9F%98%80. Both encodings agree on this. They differ only in how they write the space, and in what they do with +.
Search engines and duplicate URLs
For SEO, two URLs that differ only in %20 versus + can be treated as separate pages, so a site that links to both forms of the same address can split signals or create duplicates. Pick one form, link to it consistently and redirect the other. Better still, avoid spaces in your own URLs altogether. Use hyphens between words in paths, which Google's URL guidance recommends over underscores, and the whole question never comes up.
Where it goes wrong
Email addresses and phone numbers. jane+news@example.com is a valid address and the plus is part of it. Put it in a query string unencoded and a form-style parser reads jane news@example.com. A phone number like +44 20 7946 0000 loses its plus the same way.
Base64 in a query string. The standard Base64 alphabet contains + and /. A token such as a+b/c== placed in a URL unencoded arrives as a b/c==, which no longer decodes. This is the reason URL-safe Base64 replaces them with - and _. If you must use standard Base64, pass it through encodeURIComponent first. The Base64 encoder has a URL-safe mode for exactly this.
Paths. In a path, + is literal. A file called a+file.mp3 and one called a file.mp3 are different files, and a link that swaps %20 for + in a path returns a 404.
What to do
- Encode each value with
encodeURIComponentbefore putting it in a URL you build by hand. It gives%20for spaces and%2Bfor plus signs, which every decoder reads correctly. - If you use
URLSearchParams, decode with it too. Keep encoder and decoder as a matched pair. - Never encode a whole URL with
encodeURIComponent. It escapes the:,/and?that give the URL its structure. Encode the parts, then assemble them. - Do not encode twice. A
%that is encoded again becomes%25, so%20turns into%2520. If you see%25in a URL, something encoded an already-encoded string.
Check a value
If you are not sure what a string looks like once encoded, or want to decode one from a log, paste it into the URL encoder. It handles whole URLs and single components separately, and lists the query parameters it finds so you can see what a server will actually receive.