HomeBlogURL Encoding (Percent-Encoding) Explained

URL Encoding (Percent-Encoding) Explained

How percent-encoding works, which characters need escaping, and how to encode and decode URLs in JavaScript, Python and other languages.

DevKit is maintained by hcatree. I wrote the tools on this site and ran the compression benchmarks myself — the script and the raw data download from the benchmarks page.

2026-11-059 min read

What is URL Encoding?

URL Encoding, also known as Percent-Encoding, is an encoding scheme that converts special characters in URLs into a safe format.

Why do we need URL encoding?

URLs can only use a subset of ASCII characters. Encoding is required when URLs contain:

  1. Non-ASCII characters: Chinese, Japanese, emoji, etc.
  2. Reserved characters: ?, &, =, #, / have special meanings in URLs
  3. Unsafe characters: Spaces, <, >, ", {, }, etc.

URL Encoding Flow

URL Encoding Rules

Basic Rules

  1. Safe characters are not encoded: A-Z, a-z, 0-9, -, _, ., ~
  2. Other characters are converted to UTF-8 bytes
  3. Each byte is represented as % followed by two hexadecimal digits

Encoding Examples

Space       → %20  (or + in query strings)
Chinese "中" → %E4%B8%AD
&          → %26
=          → %3D
?          → %3F
#          → %23
+          → %2B

Complete Example

Original URL:

https://example.com/search?q=Hello World&lang=中文

Encoded:

https://example.com/search?q=Hello%20World&lang=%E4%B8%AD%E6%96%87

Breakdown:

  • Hello World → Hello%20World (space encoded as %20)
  • 中文 → %E4%B8%AD%E6%96%87 (each Chinese character encoded as 3 bytes in percent notation)

URL Structure and Encoding Positions

A complete URL consists of multiple parts, each with different encoding rules:

https://user:pass@www.example.com:8080/path/page?q=搜索#section
  |        |         |              |    |          |       |
 scheme   user     host            port  path     query   fragment
Part Encoding Method Example
Scheme Not encoded https://
Host Uses Punycode (IDN) 中文.com → xn--fiq228c.com
Path Percent-encoding /文件 → /%E6%96%87%E4%BB%B6
Query Percent-encoding q=搜索 → q=%E6%90%9C%E7%B4%A2
Fragment Percent-encoding #部分 → #%E9%83%A8%E5%88%86

URL Encoding in Different Languages

JavaScript

// Encode a complete URL component
const encoded = encodeURIComponent("Hello World & 中文");
console.log(encoded);
// "Hello%20World%20%26%20%E4%B8%AD%E6%96%87"

// Encode a query string (doesn't encode = and &)
const queryString = encodeURI("q=Hello World&page=1");
console.log(queryString);
// "q=Hello%20World&page=1"

// Decode
const decoded = decodeURIComponent(encoded);
console.log(decoded);
// "Hello World & 中文"

Difference between encodeURI and encodeURIComponent:

Method Encoding Scope Use Case
encodeURI Doesn't encode : / ? # [ ] @ ! $ & ' ( ) * + , ; = Encode complete URLs
encodeURIComponent Encodes all special characters Encode URL parameter values
const url = "https://example.com/search?q=test&page=1";

// encodeURI: preserves URL structure
console.log(encodeURI(url));
// "https://example.com/search?q=test&page=1" (unchanged)

// encodeURIComponent: encodes all special characters
console.log(encodeURIComponent(url));
// "https%3A%2F%2Fexample.com%2Fsearch%3Fq%3Dtest%26page%3D1"

Python

from urllib.parse import quote, quote_plus, unquote

# Percent-encoding
encoded = quote("Hello World & 中文")
print(encoded)
# "Hello%20World%20%26%20%E4%B8%AD%E6%96%87"

# Query string encoding (space as +)
encoded_plus = quote_plus("Hello World")
print(encoded_plus)
# "Hello+World"

# Decode
decoded = unquote(encoded)
print(decoded)
# "Hello World & 中文"

# Encode URL parameters
from urllib.parse import urlencode
params = {"q": "搜索", "page": "1"}
query = urlencode(params)
print(query)
# "q=%E6%90%9C%E7%B4%A2&page=1"

Java

import java.net.URLEncoder;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

// Encode
String encoded = URLEncoder.encode("Hello World & 中文", StandardCharsets.UTF_8);
System.out.println(encoded);
// "Hello+World+%26+%E4%B8%AD%E6%96%87"

// Decode
String decoded = URLDecoder.decode(encoded, StandardCharsets.UTF_8);
System.out.println(decoded);
// "Hello World & 中文"

PHP

// Encode
$encoded = urlencode("Hello World & 中文");
echo $encoded;
// "Hello+World+%26+%E4%B8%AD%E6%96%87"

// Encode (preserves slashes)
$encoded = rawurlencode("Hello World & 中文");
echo $encoded;
// "Hello%20World%20%26%20%E4%B8%AD%E6%96%87"

// Decode
$decoded = urldecode($encoded);
echo $decoded;
// "Hello World & 中文"

Common Use Cases

1. Search Queries

const searchTerm = "JavaScript tutorial";
const url = `https://google.com/search?q=${encodeURIComponent(searchTerm)}`;
// "https://google.com/search?q=JavaScript%20tutorial"

2. API Request Parameters

const params = {
  name: "John Doe",
  email: "john@example.com",
  message: "Hello & Welcome!"
};

const queryString = Object.entries(params)
  .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
  .join('&');

console.log(queryString);
// "name=John%20Doe&email=john%40example.com&message=Hello%20%26%20Welcome%21"

3. File Download URLs

const filename = "Report (2026).pdf";
const downloadUrl = `/api/download?file=${encodeURIComponent(filename)}`;
// "/api/download?file=Report%20(2026).pdf"

4. Redirect URLs

const returnUrl = "https://mysite.com/dashboard?tab=settings&lang=en";
const loginUrl = `/login?redirect=${encodeURIComponent(returnUrl)}`;
// "/login?redirect=https%3A%2F%2Fmysite.com%2Fdashboard%3Ftab%3Dsettings%26lang%3Den"

History of URL Encoding

Early Internet

Initially, URLs were designed for ASCII characters only. As the internet globalized, support for other languages became necessary.

Standardization Process

Year Standard Content
1994 RFC 1630 First defined percent-encoding
1998 RFC 2396 Updated URI syntax specification
2005 RFC 3986 Current standard, clear encoding rules

UTF-8 Adoption

Early systems sometimes used other encodings (like GB2312, Shift-JIS), but now UTF-8 is universally used.

Double Encoding Issues

Sometimes URLs get encoded multiple times, causing problems:

// First encoding
const once = encodeURIComponent("Hello World");
// "Hello%20World"

// Second encoding (wrong!)
const twice = encodeURIComponent(once);
// "Hello%2520World"  (% encoded as %25)

How to avoid:

  • Only encode original values once
  • If unsure, decode first then encode
// Safe approach
function safeEncode(value) {
  try {
    // Try to decode first (if already encoded)
    const decoded = decodeURIComponent(value);
    return encodeURIComponent(decoded);
  } catch {
    // If decoding fails, it wasn't encoded
    return encodeURIComponent(value);
  }
}

Frequently Asked Questions

What's the Difference Between + and %20 in URLs?

Encoding Meaning Use Case
%20 Space URL paths, general encoding
+ Space Query strings (application/x-www-form-urlencoded)

In query strings, both are usually equivalent. But in paths, only %20 can be used.

Why is Chinese Encoding So Long?

Because in UTF-8, each Chinese character takes 3 bytes, and each byte is encoded as %XX (3 characters):

1 Chinese character = 3 bytes = 9 encoded characters
"中文" = 2 characters → "%E4%B8%AD%E6%96%87" = 18 characters

That's about 9x size inflation!

When is Encoding Not Needed?

These characters are safe in URLs and don't need encoding:

  • Letters: A-Z, a-z
  • Numbers: 0-9
  • Special characters: -, _, ., ~

What's the Difference Between URL Encoding and HTML Entity Encoding?

Feature URL Encoding HTML Entity Encoding
Purpose Special characters in URLs Special characters in HTML
Format %XX &xxx; or &#xxx;
Example Space → %20 < → &lt;
Standard RFC 3986 HTML5

Best Practices

  1. Always encode user input: Don't trust user-provided URL parameters
  2. Use standard library functions: Don't manually concatenate encoding
  3. Watch encoding count: Avoid double encoding
  4. Distinguish encodeURI from encodeURIComponent: Choose based on context
  5. Use UTF-8 consistently: Don't use other character sets

Use the DevKit URL Encoder/Decoder Tool

DevKit provides an online URL encoding/decoding tool supporting:

  • Instant encoding and decoding
  • UTF-8 and multilingual character support
  • Batch processing
  • Automatic encoding format detection

Try the URL Encoder/Decoder →

Discuss this articleEchoForum · no sign-up, replies in any language

Related Tools

URL EncoderEncode and decode URLs