URL Encoding and Decoding

Web

URL encoding is the process of converting characters into a format that can be safely included in a URL. Some characters have special meanings in URLs or cannot be used directly, so they are replaced with encoded values.

This is commonly called percent encoding because encoded characters usually begin with a % followed by two hexadecimal digits. URL decoding performs the reverse operation, converting those encoded values back into their original characters.

How it Works

A URL may contain information such as search terms, filenames, or other values supplied by a user. Characters such as spaces, &, ?, #, and / can have special meanings inside a URL, so applications encode them when they need to be treated as ordinary data.

For example:

Original:
hello world

Encoded:
hello%20world

A few common examples are:

Space        → %20
&            → %26
#            → %23
/            → %2F
?            → %3F
=            → %3D

Suppose we want to include the search term:

cats & dogs

Directly placing it into a URL could cause the & to be interpreted as part of the URL structure:

https://example.com/search?q=cats & dogs

Encoding the value produces:

https://example.com/search?q=cats%20%26%20dogs

The receiving application can then decode:

cats%20%26%20dogs

back into:

cats & dogs

Browsers and programming languages usually handle much of this encoding and decoding automatically. It is still important to understand what is happening when inspecting URLs, working with query parameters, or debugging an API.

See More