Have you ever noticed that when you search for something with spaces in it, those spaces somehow transform into %20 in your browser's address bar? That transformation is called URL encoding, also known as percent-encoding. It's one of those invisible technologies that makes the modern web work, handling the messy reality that URLs can only contain a limited set of characters while the data we want to transmit is far more varied.

URLs were designed for specific purposes, and the original specification only allowed a subset of ASCII characters. Spaces, ampersands, question marks, and dozens of other characters have special meaning in URLs or aren't allowed at all. URL encoding provides a way to represent any character as a sequence of safe characters, ensuring that text data can be transmitted through the URL system without breaking anything.

Why URLs Need Encoding

URLs have a specific structure with reserved characters that serve particular functions. The question mark separates the path from the query string. Ampersands separate query parameters. Slashes separate path segments. Hash marks indicate fragments. These characters mean something specific, so if your data contains them, you can't simply drop them into a URL raw—they would be interpreted as structural elements rather than content.

Consider a search for "coffee & donuts" at an online store. If we put that directly in a URL without encoding, we'd have ?search=coffee & donuts. The browser sees the space after "coffee" as a parameter separator, and the ampersand as a query parameter separator. The URL becomes garbled—coffee becomes a parameter with value empty, and donuts becomes another parameter with no value.

Encoding transforms that into ?search=coffee%20%26%20donuts. The %20 represents a space, and %26 represents the ampersand. Now the URL passes through the system intact and the server correctly receives "coffee & donuts" as the search term.

How Percent-Encoding Works

The encoding format is straightforward: a percent sign followed by two hexadecimal digits representing the character's byte value. Since most web content uses UTF-8 encoding, a character might be represented by multiple bytes, each requiring its own percent-encoded pair. A single emoji like 😀 requires three byte pairs: %F0%9F%98%80.

Not every character requires encoding. Letters, numbers, hyphens, underscores, tildes, and periods are generally "unreserved" and safe as-is. This is why you can have URLs like example.com/my-page without any special handling. Everything else typically needs encoding, though some characters are allowed in specific URL contexts.

The rules differ depending on where in the URL a character appears. Within the path portion, forward slashes are delimiters so they must be encoded if they appear in actual path content. Within query parameters, spaces must be encoded but the ampersand separating parameters must not be. These contextual rules add complexity, which is why most frameworks provide utilities to handle URL encoding correctly rather than requiring developers to memorize every rule.

Common Character Encodings

Space is probably the most commonly encountered encoded character, appearing as %20 in URLs. You might also see plus signs (+) used in some contexts, particularly legacy form submissions where spaces in query strings were traditionally encoded as + rather than %20. Modern practice generally prefers %20 everywhere, but you may encounter the plus sign convention.

Common special characters and their encoded forms include: %21 for exclamation mark, %23 for hash/pound, %24 for dollar sign, %26 for ampersand, %2B for plus, %3D for equals, %3F for question mark, %2F for forward slash, and %3A for colon. When building URLs programmatically, these escape sequences come up constantly.

Non-ASCII characters like accented letters, Chinese characters, or emoji require UTF-8 encoding followed by percent-escaping. A Chinese phrase like 你好 becomes %E4%BD%A0%E5%A5%BD. This is why international domain names and URLs with non-English content look so unusual—they're all percent-encoded representations of Unicode characters.

How Browsers Handle Encoding

Modern browsers generally handle URL encoding automatically behind the scenes. When you type a search query in a form field and submit it, the browser encodes the data appropriately for you. When you click a link, the browser handles encoding. You rarely need to think about it—until something goes wrong or you need to construct a URL manually.

Browsers also display URLs decoded in the address bar for readability, even though the actual transmission uses encoded values. You see coffee & donuts but the browser sends coffee%20%26%20donuts. This display behavior sometimes confuses people debugging URL issues—they're looking at human-readable form rather than the actual transmitted form.

Copying URLs from the browser address bar typically gives you the encoded version. However, some applications extract readable text from URLs for display purposes, which can cause problems when that readable text gets re-used as if it were a valid URL. Always use proper URL encoding functions rather than trying to manually construct or parse URLs.

Frequently Asked Questions

Should I use %20 or + for spaces in URLs?

The current specification (RFC 3986) prefers %20 for all URL components including query strings. The plus sign convention for spaces comes from older form submissions (application/x-www-form-urlencoded) and is technically deprecated for general URL construction. For modern web development, %20 is the correct choice. Some server-side frameworks still decode + as space in query parameters for legacy compatibility.

Are uppercase or lowercase hex digits correct in encoding?

Both work—%2f and %2F are equivalent and any conformant decoder should accept both. However, RFC 3986 specifies uppercase hex digits in its examples and most implementations follow this convention. For consistency, using uppercase is generally preferred, though the practical difference is negligible.

Can I encode just part of a URL?

Yes, different parts of a URL have different encoding rules. Path segments, query parameters, and fragment identifiers each have their own rules about which characters must be encoded and which are allowed literally. Query parameter names and values are encoded separately. This is why you should use framework-provided URL building utilities rather than string concatenation—handling all these rules correctly is more complex than it appears.

Why do some URLs with special characters work without encoding?

Browsers are forgiving and perform "URI salvageaging"—attempting to interpret malformed URLs in reasonable ways. A space might be automatically encoded when the browser sends the request, even if it appeared raw in the address bar. However, relying on this behavior is poor practice. URLs should be properly encoded from the start. Different browsers may handle malformed URLs differently, leading to inconsistent behavior.