URL encoding (also called percent encoding) converts characters that are not safe to use in URLs into a percent sign followed by two hexadecimal digits. It is required whenever a URL contains spaces, special characters, or non-ASCII characters.
What URL encoding is
A URL can only contain a limited set of characters: letters (A–Z, a–z), digits (0–9), and a small set of special characters (-_.~). All other characters must be percent-encoded.
Percent encoding replaces each unsafe character with a % followed by its two-digit hexadecimal ASCII code. For example:
• Space → %20
• & → %26
• = → %3D
• # → %23
• / → %2F (when used as data, not a path separator)
Why URL encoding is needed
URLs have a defined syntax. Characters like ?, &, =, and # have special roles in URL structure. If your data contains these characters (e.g. a search query containing "price=high&color=red"), they must be encoded to prevent the URL parser from interpreting them as structural characters.
Without encoding, a URL like:
https://example.com/search?q=coffee & tea
…would break in most browsers and HTTP clients because the space and & are not properly escaped.
When to encode the full URL vs. just the value
Encode query string values, not the full URL. If you encode the entire URL, you will encode the slashes, colons, and question marks that are part of the URL structure itself.
Correct:
https://example.com/search?q=coffee%20%26%20tea
Incorrect (over-encoded):
https%3A%2F%2Fexample.com%2Fsearch%3Fq%3Dcoffee%20%26%20tea