URL Encoding (Percent-Encoding) Explained with Examples

6 min read · Published 1 November 2026 · Updated 2 November 2026

A URL can contain only a limited set of characters. Anything else, including spaces, accented letters and characters that have a special job in a URL, has to be written in a safe form called percent-encoding. Getting it wrong produces broken links, lost query parameters and, in the worst case, security bugs. This guide explains the rules and the usual mistakes.

What percent-encoding is

Percent-encoding replaces a byte with a percent sign followed by two hexadecimal digits. A space is the byte 0x20, so it becomes %20. Characters outside basic ASCII are first converted to bytes using UTF-8 and each byte is then encoded. For example, the Vietnamese letter "ế" is three bytes in UTF-8 (E1 BA BF), so it becomes %E1%BA%BF.

Unreserved and reserved characters

The URL standard (RFC 3986) divides characters into groups:

  • Unreserved characters never need encoding: letters A to Z and a to z, digits 0 to 9, and the symbols - . _ ~
  • Reserved characters have a special meaning as separators: : / ? # [ ] @ and ! $ & ' ( ) * + , ; =
  • Everything else, including spaces, quotes, non-ASCII letters and control characters, must be percent-encoded.

The context matters

A reserved character is left alone when it is doing its separating job, and must be encoded when it is part of the data. The ? that starts a query string stays as it is. A question mark inside a search term must become %3F. Likewise & separates parameters, so an ampersand inside a value must be written %26, and a literal # inside a value must be %23, or the browser treats everything after it as a fragment.

encodeURI versus encodeURIComponent

JavaScript has two functions, and choosing the wrong one is a frequent bug. encodeURI encodes a whole URL and deliberately leaves reserved characters such as : / ? # & = alone, so the address keeps its structure. encodeURIComponent encodes a single piece of data (a parameter value, for example) and encodes those reserved characters too. Use encodeURIComponent for values you insert into a URL.

JavaScript

encodeURIComponent("a b&c=d")   // "a%20b%26c%3Dd"
encodeURIComponent("ế")         // "%E1%BA%BF"

// Safer: let the platform build the query string
new URLSearchParams({ q: "a b&c" }).toString()   // "q=a+b%26c"

Space: %20 or +?

In a URL path, a space is %20. In the query string of an HTML form submission (the application/x-www-form-urlencoded format) a space may be written as +. Both are understood by servers in a query string, but a + in a path is a literal plus sign. URLSearchParams uses +, while encodeURIComponent uses %20. If you mix them up you may see plus signs where spaces should be, or the reverse.

Double encoding

Encoding a string that is already encoded turns % into %25, so %20 becomes %2520 and the server sees a literal "%20" instead of a space. It usually happens when several layers of code each "helpfully" encode the value. Encode exactly once, at the point where you build the URL, and decode exactly once when you read it.

Security angle

Never put untrusted text into a URL without encoding it, and never trust a decoded value. Attackers use encoding tricks to slip characters past filters, for example %2e%2e%2f for ../ in path traversal, or %0d%0a to inject line breaks into headers. Decode first, then validate. Also check that redirect targets taken from a URL parameter point only to places you allow.

URLs in QR codes

A QR code stores the exact text you give it. If the link contains spaces or special characters that are not encoded, many scanners and browsers will fix it in different ways, or fail. Encode the link first, and keep it short, because each additional character makes the code denser and harder to scan.

Frequently asked questions

Is percent-encoding the same as encryption?
No. It is a reversible way of writing characters so that a URL stays valid. It offers no confidentiality.
Do I need to encode the slashes in a path?
Slashes that separate path segments stay as they are. A slash that is part of a single segment's data, for example an ID that contains one, must be encoded as %2F.
Why did my link break after a # sign?
In a URL, # starts the fragment, which is never sent to the server. If a # is part of your data, encode it as %23.
How do I decode a percent-encoded string?
Use decodeURIComponent() in JavaScript or the equivalent function in your language, and decode only once. Be ready to handle an error if the input contains an invalid sequence.

Sources and further reading

Try it yourself: QR Code Generator runs in your browser, free and with no sign-up.

More guides