What Is URL Encoding (Percent-Encoding)?

urlencodingweb

URLs can only safely contain a limited set of characters. URL encoding, also called percent-encoding, replaces reserved or unsafe characters with a percent sign followed by two hexadecimal digits.

Why URLs need encoding

A space, a slash, or a question mark can change how a URL is parsed. Encoding turns those characters into unambiguous text so the URL works as intended in links, query strings, and API paths.

Reserved versus unreserved

  • Unreserved characters such as letters, digits, -, _, . and ~ can stay as-is.
  • Reserved characters such as ?, &, = and / have special meaning and must be encoded when they are data.
  • A space is commonly encoded as %20.

encodeURI versus encodeURIComponent

encodeURI keeps URL structure such as slashes and question marks intact, which is useful for a whole URL. encodeURIComponent escapes more characters and is the right choice for a single query value.

Encoding in code

JavaScript:

console.log(encodeURIComponent('hello world & more'))
// hello%20world%20%26%20more

Python:

from urllib.parse import quote
print(quote('hello world & more'))

PHP:

echo rawurlencode('hello world & more');

Common pitfalls

  • Encoding a value twice, which turns % into %25.
  • Using + for spaces when the consumer expects %20.
  • Encoding a full URL with encodeURIComponent when only a single value should be escaped.

Frequently asked questions

  • What is the difference between URL and component encoding? Component encoding escapes more characters and is for query values.
  • Is my input uploaded? No. Encoding runs locally in your browser.

Need to encode or decode a URL? Try the free URL Encoder / Decoder — it runs entirely in your browser.