URL Encoding Explained: What It Is and Why It Matters
A space in a URL breaks it. So does an ampersand in the wrong place, or a slash where one wasn't expected. URL encoding exists to make any arbitrary text safe to embed inside a URL without confusing the parser that reads it.
What percent-encoding does
URL (percent) encoding replaces unsafe or reserved characters with a percent sign followed by their hexadecimal code — a space becomes %20, an ampersand becomes %26. This lets arbitrary text, including punctuation and non-English characters, travel safely inside a URL without being misread as part of its structure.
Why certain characters need encoding
URLs use specific characters structurally: ? marks the start of a query string, & separates parameters, / separates path segments, # marks a fragment. If your actual data contains one of these characters unencoded, the URL parser misinterprets it as structure rather than content — which is exactly how a search query containing "&" can silently truncate or corrupt a URL.
A concrete example
| Original text | URL-encoded |
|---|---|
| hello world | hello%20world |
| price&discount | price%26discount |
| 50% off | 50%25%20off |
Note the last row: the percent sign itself needs encoding too (to %25), since it's the character that signals encoding in the first place.
The double-encoding trap
Encoding an already-encoded string produces garbled output — %20 encoded again becomes %2520, not a space. This happens surprisingly often when a value passes through multiple layers of code, each assuming it needs to encode from scratch. The fix isn't complicated, but it requires knowing at each step whether the value is already encoded.
Frequently asked questions
Only the variable parts — like a query parameter's value — need encoding. Encoding the structural parts of a URL (like "https://" or "/") would break it, since those characters are meant to remain literal.
encodeURIComponent encodes nearly everything except a small safe set, meant for encoding a single value (like a query parameter); encodeURI leaves URL-structural characters like / and ? untouched, meant for encoding a full URL that should stay functional.
Conclusion
URL encoding is a narrow, mechanical fix for a specific problem: making arbitrary text safe inside a structured format. Encode values, not whole URLs, and watch for double-encoding when data passes through multiple systems.
Try it with the URL Encoder / Decoder.