Guides

URL Encoding Explained: Understanding Percent Encoding [Developer Guide]

7 min read
January 24, 2026
url encoding, percent encoding, url encode decode

Ever seen %20 in URLs and wondered what it means? URL encoding (percent encoding) makes special characters safe for URLs.

This guide explains why URL encoding exists, how it works, and common pitfalls to avoid.

Why URL Encoding Exists#

The Problem: URLs can only contain certain characters. Spaces, special symbols, and non-ASCII characters break URLs.

Example invalid URL:

https://example.com/search?q=hello world&user=john@doe

Problems:

  • Space breaks the URL
  • @ has special meaning
  • & separates query parameters

The Solution: Encode special characters as %XX where XX is the hexadecimal ASCII code.

Fixed URL:

https://example.com/search?q=hello%20world&user=john%40doe

Reserved Characters#

Characters with Special Meaning in URLs:

CharacterPurposeEncodedExample Use
:Scheme separator%3Ahttp:
/Path separator%2F/path/to
?Query start%3F?key=value
#Fragment%23#section
&Param separator%26a=1&b=2
=Key-value separator%3Dkey=value
@User info%40user@

How Percent Encoding Works#

The Process:

  1. Take the character's ASCII or UTF-8 code
  2. Convert to hexadecimal
  3. Prefix with %

Example: Space character

  • ASCII code: 32 (decimal)
  • Hexadecimal: 20
  • Encoded: %20

Example: @ symbol

  • ASCII code: 64
  • Hexadecimal: 40
  • Encoded: %40

UTF-8 Characters (multiple bytes):

€ symbol → UTF-8: 0xE2 0x82 0xAC → Encoded: %E2%82%AC

The Space Character: %20 vs +#

Two Ways to Encode Spaces:

  • %20 — Proper percent encoding
  • + — Application/x-www-form-urlencoded (query strings)

When to Use Each:

  • Path: Always use %20 (/my%20file.pdf)
  • Query string: Either works, but %20 is safer (?name=John%20Doe)

Example:

// Both are valid in query strings
?search=hello+world
?search=hello%20world

// Only %20 works in paths
/files/my%20document.pdf  ✅
/files/my+document.pdf    ❌

Common Mistakes#

❌ Mistake #1: Double Encoding

// Wrong: encoding twice
encodeURIComponent(encodeURIComponent("hello world"))
// Result: hello%2520world (broken!)

❌ Mistake #2: Not Encoding Query Params

// Wrong
const url = `?email=${email}`;  // Breaks if email has @ or &

// Correct
const url = `?email=${encodeURIComponent(email)}`;

❌ Mistake #3: Encoding the Entire URL

// Wrong: breaks the URL structure
encodeURIComponent("https://example.com/path")
// Result: https%3A%2F%2Fexample.com%2Fpath (broken!)

// Correct: only encode the dynamic parts
`https://example.com/path?q=${encodeURIComponent(query)}`

Implementation Examples#

JavaScript:

// Encode a query parameter
const encoded = encodeURIComponent("hello world");
// "hello%20world"

// Decode
const decoded = decodeURIComponent("hello%20world");
// "hello world"

// Full URL encoding
const params = new URLSearchParams({
  search: "hello world",
  email: "user@example.com"
});
console.log(params.toString());
// "search=hello+world&email=user%40example.com"

Python:

from urllib.parse import quote, unquote

# Encode
encoded = quote("hello world")
# 'hello%20world'

# Decode
decoded = unquote("hello%20world")
# 'hello world'

PHP:

# Encode
$encoded = urlencode("hello world");
// "hello+world"

$encoded = rawurlencode("hello world");
// "hello%20world" (RFC 3986 compliant)

// Decode
$decoded = urldecode("hello%20world");

Frequently Asked Questions

What does %20 mean in a URL?
%20 is the URL-encoded representation of a space character. It comes from the ASCII code for space (32 in decimal, 20 in hexadecimal), prefixed with %.
When should I use + vs %20 for spaces?
Use %20 everywhere for consistency and RFC compliance. The + is an older encoding for application/x-www-form-urlencoded (HTML forms). %20 works in both paths and query strings.
Should I encode the entire URL?
No! Only encode the dynamic parts (query parameters, path segments with special chars). Encoding the entire URL breaks the structure (https%3A%2F%2F becomes invalid).
What is the difference between encodeURI and encodeURIComponent?
encodeURI encodes a full URL, preserving :, /, ?, &. encodeURIComponent encodes individual components, encoding those special chars too. For query params, always use encodeURIComponent.
Do I need to encode non-ASCII characters?
Yes! Characters outside the ASCII range (like é, ñ, 中) must be UTF-8 encoded then percent-encoded. Modern browsers handle this automatically, but backend APIs may require explicit encoding.

Encode & Decode URLs Instantly

Use our free URL encoder to safely encode special characters for URLs. Supports encoding and decoding.

Try URL Encoder

Related Articles