Developers · 6 min read

URL Encoding Explained: Percent-Encoding, Spaces and Special Characters

A URL can only safely carry a limited set of characters. Everything else, from spaces to accented letters to the ampersand inside a search term, has to be escaped so that it cannot be mistaken for URL structure. That escaping is called percent-encoding. Getting it wrong produces broken links, lost query parameters and hard-to-trace bugs, so here is how it actually works.

What percent-encoding does

Percent-encoding replaces a character with a percent sign followed by the two hexadecimal digits of its byte value. A space is the byte 0x20, so it becomes %20. A literal ampersand is 0x26, so it becomes %26.

Characters outside plain ASCII are first converted to UTF-8 bytes, and each byte is then encoded. The letter é is two bytes, so it becomes %C3%A9, and an emoji can take four bytes and therefore twelve characters of encoded text.

Which characters are safe and which are reserved

The URL standard (RFC 3986) defines unreserved characters that never need encoding: letters, digits, and the four symbols - . _ ~. Everything else is either reserved, meaning it has a structural role, or must always be encoded.

  • Reserved characters such as : / ? # [ ] @ ! $ & ' ( ) * + , ; = separate the parts of a URL. Inside a value they must be encoded so they are not read as structure.
  • A literal # in a value must be %23, or everything after it is treated as the fragment and never reaches the server.
  • A literal & or = inside a query value must be %26 and %3D, or the parameter splits in the wrong place.
  • Characters such as spaces, quotes, angle brackets and non-ASCII letters always need encoding.

encodeURI vs encodeURIComponent

JavaScript offers two functions, and using the wrong one is the most common mistake. encodeURIComponent escapes nearly everything, including / ? & = and #, so use it for a single value you are placing into a URL, such as one query parameter.

encodeURI leaves the structural characters alone so that a complete address keeps working. Use it only on a whole URL that is already assembled, never on an individual value.

  • Building a query string: encode each key and value with encodeURIComponent, then join with = and &.
  • Cleaning up a full address that contains spaces: encodeURI is appropriate.
  • Encoding a value that contains a URL, for example a redirect target: encodeURIComponent, so the inner ? and & do not leak into the outer URL.

Spaces: %20 or +?

In a URL path, a space is always %20. In a query string a plus sign is also widely understood as a space, but only because of the older application/x-www-form-urlencoded format used by HTML forms. A plus sign in a path is just a plus sign.

Because of that ambiguity, %20 is the safer choice everywhere, and a literal plus sign that you really mean should be encoded as %2B.

Double encoding and other common bugs

Double encoding happens when an already-encoded value is encoded again. The % in %20 becomes %25, giving %2520, and the receiving server decodes it once to get the literal text %20 instead of a space. Encode exactly once, at the point where you build the URL.

Decoding can fail too. A percent sign that is not followed by two hex digits, or a truncated multi-byte sequence, is malformed and throws an error in most languages. Decode untrusted input defensively.

  • Encode at the boundary where you assemble the URL, not earlier and not repeatedly.
  • Never build a URL by concatenating raw user input, because an unescaped & or # changes its meaning.
  • When something looks wrong, paste the value into a decoder: if you still see %25 or %2520, it has been encoded more than once.

Frequently asked questions

Do I need to encode the whole URL?
Usually not. Encode the individual pieces you insert, such as a query value or a path segment, and leave the structural characters of the URL alone. Encoding a whole URL with encodeURIComponent turns its slashes and colons into escape codes and breaks it.
Is URL encoding the same as encryption?
No. Percent-encoding is a reversible formatting step that anyone can undo. It provides no secrecy, so never rely on it to hide sensitive data.
Why do short links and tracking URLs contain so many %-codes?
Query values often contain characters such as spaces, commas and other URLs, and each of those is escaped so the link survives being copied, pasted and redirected. UTM parameters with spaces or symbols are encoded the same way.

More guides