Skip to main content
Back to Blog

URL Encoding & Decoding: Definitive Guide

Percent-encoding keeps URLs and query strings safe; here’s when to encode, when to decode, and how to avoid common pitfalls.

Kashyap Thakar
5 min
URLWeb StandardsEncoding
URL Encoding & Decoding: Definitive Guide

We see them every day: those strange %20 and %3F sequences cluttering our browser address bars. But what exactly is URL encoding, and why is the internet built on top of it?

The ASCII Limitation

The internet was born in an era where US-ASCII was the standard. URLs were designed to only support a specific subset of characters: alphanumerics and a handful of special symbols like -, _, ., and ~.

But the world is rich with diverse languages, emojis, and complex data. How do you put a space, a Cyrillic letter, or a query parameter containing `&` into a URL without breaking it? Enter Percent-Encoding.

How Percent-Encoding Works

Percent-encoding is a mechanism for encoding information in a Uniform Resource Identifier (URI). Characters are replaced by a % followed by their two-digit hexadecimal value.

CharacterEncoded ValueReason
Space%20Unsafe in URLs
/%2FPath separator
?%3FQuery string start
&%26Parameter separator

Common Pitfalls

Double Encoding

A frequent bug occurs when a string is encoded twice. Hello World becomes Hello%20World, which if encoded again becomes Hello%2520World (since % becomes %25). Decoding this once leaves you with the still-encoded string, leading to confusing bugs.

Encoding the Entire URL

You should generally only encode the values of query parameters, not the entire URL string. Encoding the :// or the ? will break the browser's ability to parse the protocol and path.

Real-World Examples

Example 1: Search Query with Special Characters

Imagine you're building a search feature and a user searches for "C++ tutorial". The plus signs need to be encoded:

Original: C++ tutorial
Encoded: C%2B%2B+tutorial
URL: https://example.com/search?q=C%2B%2B+tutorial

Example 2: International Characters

URLs with non-ASCII characters must be encoded. For example, "café" becomes:

Original: café
Encoded: caf%C3%A9
URL: https://example.com/place?name=caf%C3%A9

Example 3: Query Parameters with Multiple Values

When building complex query strings, each parameter value must be encoded separately:

Original: category=electronics&search=phone & charger
Encoded: category=electronics&search=phone%20%26%20charger
URL: https://shop.com/products?category=electronics&search=phone%20%26%20charger

Encoding in Different Languages

Most programming languages provide built-in functions for URL encoding:

// JavaScript
encodeURIComponent("hello world") // "hello%20world"
decodeURIComponent("hello%20world") // "hello world"

// Python
from urllib.parse import quote, unquote
quote("hello world") // "hello%20world"
unquote("hello%20world") // "hello world"

// PHP
urlencode("hello world") // "hello+world"
urldecode("hello+world") // "hello world"

Best Practices

  • Always encode query parameter values: Never trust user input. Always encode values before constructing URLs programmatically.
  • Use the right function: encodeURI() encodes the entire URL (preserving :// and /), while encodeURIComponent() encodes individual components.
  • Handle decoding errors gracefully: Malformed encoded strings can cause exceptions. Always wrap decode operations in try-catch blocks.
  • Be consistent: Use the same encoding method throughout your application to avoid double-encoding issues.

Tools of the Trade

While most programming languages offer built-in functions like encodeURIComponent() in JavaScript, having a quick visual tool is essential for debugging. When you're troubleshooting a broken URL or verifying that your encoding logic is correct, a visual tool provides immediate feedback.

Our URL Encoder/Decoder handles these conversions instantly, helping you verify that your query strings are formatted correctly before they hit your codebase. It's particularly useful for:

  • Quick debugging of malformed URLs
  • Testing edge cases with special characters
  • Verifying encoding behavior across different scenarios
  • Learning how different characters are encoded

Part of the ThenCatch blog. Learn more about us or browse more guides.