What Is URL Encoding?

URL encoding (percent encoding) replaces unsafe or reserved characters in a URL with a percent sign and their hexadecimal value.

Why URLs need encoding at all

URLs are restricted to a limited set of safe characters. Certain characters are reserved because they have structural meaning — `?` starts a query string, `&` separates parameters, `=` separates a key from a value, `/` separates path segments, and `#` starts a fragment. Any time you need one of these characters (or a space, or a non-ASCII character) to appear as literal data rather than structural punctuation, it needs to be encoded.

How percent encoding works

Percent encoding replaces an unsafe character with `%` followed by its two-digit hexadecimal byte value. A space becomes `%20`, an ampersand becomes `%26`. Non-ASCII characters are first converted to their UTF-8 byte representation, then each byte is percent-encoded, which is why a single accented letter or emoji can turn into several `%XX` sequences in a row.

Encoding a value vs. encoding a whole URL

There's an important distinction between encoding an individual value that will be placed inside a URL (which should escape nearly everything, including reserved characters, since the value might legitimately contain them) versus encoding a complete, already-structured URL (where reserved characters like `/` and `?` should be preserved to keep the URL's structure intact). Mixing these up is a common source of broken links.

Where you'll use this in practice

You'll reach for URL encoding when building query strings dynamically from user input, embedding one URL as a parameter inside another (like a redirect target), or debugging why a link containing special characters isn't working as expected.