URL encoding pitfalls: %20 or +, double encoding, and the characters that break links

Try it in the tool

URL Encode / Decode

Percent-encode, decode and parse URLs and query strings.

URL Parser

See every part of a URL, decoded, with the gotchas pointed out.

A URL is a very old format, and it can carry only a small set of ASCII characters. Everything else, from a space to the letter ễ, has to be written as percent-escapes. The rules are short, but the same few mistakes appear in almost every codebase: a plus sign that becomes a space, an ampersand that splits a value in two, an escape that was applied twice. This article shows what actually happens to the bytes.

The rule: percent-escape the UTF-8 bytes

Percent-encoding takes the UTF-8 bytes of a character and writes each one as % followed by two hexadecimal digits. A space is one byte, 20, so it becomes %20. A character outside ASCII is several bytes, so it becomes several escapes. Try it in the URL encoder:

Text Encoded
Nguyễn Văn A Nguy%E1%BB%85n%20V%C4%83n%20A
Hà Nội H%C3%A0%20N%E1%BB%99i

In the first row, ễ is the three bytes E1 BB 85, and ă is the two bytes C4 83. So accents make a URL longer than the character count suggests, and the hex digits are written in upper case by convention.

Three encoders, three jobs

The URL encoder has three styles, and picking the wrong one is the most common mistake.

  • Component is encodeURIComponent: use it for one value that you are about to put into a URL, such as a search term. It escapes the characters that give a URL its structure, such as : / ? # & = and +, along with spaces and non-ASCII characters.
  • Full URI is encodeURI: use it only to repair a whole URL that has spaces or accents in it. It keeps ; , / ? : @ & = + $ # because they give the URL its structure.
  • Form (+) is the style of HTML forms and URLSearchParams: a space becomes +, and ! ' ( ) and ~ are escaped too.

The same input shows the differences:

Input Component Full URI Form (+)
a b&c=d a%20b%26c%3Dd a%20b&c=d a+b%26c%3Dd
1+1=2 1%2B1%3D2 1+1=2 1%2B1%3D2
https://example.com/a b?x=1&y=ü#top https%3A%2F%2Fexample.com%2Fa%20b%3Fx%3D1%26y%3D%C3%BC%23top https://example.com/a%20b?x=1&y=%C3%BC#top https%3A%2F%2Fexample.com%2Fa+b%3Fx%3D1%26y%3D%C3%BC%23top

The last row shows both mistakes. Encoding a whole URL as a component turns https:// into https%3A%2F%2F, which is no longer a URL. Encoding a value as a full URI leaves its & and = untouched, and they then split or rename parameters. The rule of thumb is: encode each value as a component, then join the pieces; use the full-URI style only to clean up a URL you were given.

Bug 1: a plus sign or %20

The form style writes a space as +, and that is why new URLSearchParams({ q: 'a b' }).toString() gives q=a+b. But the plus sign is only a space in the query string of a form. In a path it is an ordinary character.

The decoder shows both readings of a+b: in component style it stays a+b, in form style it becomes a b. That is the source of a nasty bug: a value that contains a real plus sign, such as 1+1=2, must be sent as 1%2B1%3D2. Sent raw, a form decoder reads it as 1 1=2. Whenever a value can contain +, encode it as a component.

When you cannot be sure which side decodes the URL, %20 is the safe choice for a space, because every decoder reads it the same way.

Bug 2: double encoding

%2520 in a log line is the signature of double encoding. The first pass encoded a space as %20. The second pass saw the % and encoded it as %25, giving %2520. Decoding it once returns the literal text %20, and only a second decode returns the space.

It usually happens when a client encodes a value and a framework, proxy or HTTP library encodes the whole URL again. The fix is not to decode twice on the server, which hides the bug and opens the door to confusion about what a value means. Encode exactly once, at the boundary where the URL is built, and let the library own that step.

Bug 3: malformed escapes and cut characters

A percent sign must be followed by two hexadecimal digits. Decoding 100% or 100%zz fails with a precise message: “Malformed escape at position 4: ‘%’ must be followed by two hex digits”. A literal percent sign in a value must therefore be written %25.

Multi-byte characters bring a second failure. %E1%BB%85 decodes to ễ, but a URL cut after the second escape, %E1%BB, is a half character, and the decoder reports an invalid UTF-8 byte sequence. A length limit that truncates a URL in the middle of an escape produces exactly this. The same family of error exists on the encoding side: encodeURIComponent('\uD800') throws URIError: URI malformed, because a lone surrogate is not a valid character.

Bug 4: encoding the wrong layer

A URL has structure, and the characters that mark it are reserved. In a query string & separates parameters, = separates a name from its value, and # begins the fragment. So ?q=a&b means a parameter q with the value a and a second parameter b. To send the text a&b, the value must be ?q=a%26b.

The URL parser makes this visible. Paste a URL such as https://user:pa%24s@example.com:8080/a%20b/c?x=1&x=2&y=%E2%9C%93&q=a+b#frag%20ment and it splits it into parts: the path /a b/c decoded, the parameter x with two values, y as ✓, q as a b (the plus was read as a space in the query), and the fragment frag ment. The password is masked rather than printed. When a link behaves strangely, reading its parts side by side usually shows which layer was encoded wrongly.

  • Base64 in a URL. Standard Base64 contains + and /, which a URL treats specially. Use the URL-safe alphabet with - and _, or percent-encode the value. See Base64 is not encryption for the details.
  • Parsing URLs with a regular expression. A pattern that handles every case grows fragile and can backtrack badly (see regex backtracking). The URL parser built into the platform does the job correctly.
  1. Is the value encoded exactly once? Look for %25 followed by two digits.
  2. Is each value encoded as a component before being joined into the URL?
  3. Could a value contain +, &, =, # or %? Those need escaping.
  4. Does the decoder treat + as a space (form) or as a plus (path)?
  5. Was the URL cut in the middle of an escape?
  6. Paste the whole URL into a parser and read the parts.

Everything you paste into these tools is processed in your browser and never uploaded.

Frequently asked questions

Should a space be %20 or +?

%20 is always safe. A plus sign means a space only in form-style query strings (application/x-www-form-urlencoded), the format of HTML forms and URLSearchParams. In a path, a plus sign is just a plus. If you are not sure who will read the URL, use %20 and never put a raw + in a value.

What is the difference between encodeURI and encodeURIComponent?

encodeURI fixes a whole URL: it escapes spaces and non-ASCII characters but keeps the characters that give a URL its structure, such as : / ? # & and =. encodeURIComponent is for one value that you are about to put inside a URL: it escapes all of those too. Using the first on a value, or the second on a whole URL, are the two classic mistakes.

Why do I see %2520 in my URL?

The URL was encoded twice. The first pass turned a space into %20, and the second pass turned the percent sign of that escape into %25, giving %2520. Decode it once and you get the literal text %20, decode it twice and you get the space. Encode exactly once, at the point where the URL is built.

Why does my decoder report an invalid UTF-8 sequence?

Percent-encoding works on the bytes of UTF-8 text, and one character can be up to four bytes. If a URL was cut in the middle of a character, for example by a length limit, the last escape is an incomplete sequence like %E1%BB. Decoding fails because those bytes are not a valid character.

Do I have to encode characters like ~ and * in a URL?

Not in a path or query: they are allowed or reserved but harmless. The form style used by HTML forms is stricter and also escapes ! ' ( ) and ~. Both forms decode to the same text, so you only need to match what the receiving server expects.

More guides