URL Encoding Guide: Safe Characters and Special Cases
Master URL encoding. Learn which characters need encoding, practical examples, and how to use URL encoders safely.

Table of Contents
URLs should only contain specific characters. When you need to include special characters, URL encoding is the solution. This guide teaches you everything you need to know.
What is URL Encoding?
Definition
URL encoding (also called percent-encoding) converts special characters into a safe format that URLs can transmit.
Original: Hello World! Encoded: Hello%20World%21 Original: [email protected] Encoded: user%40example.com
Why It's Needed
URLs have restrictions:
- Space character can break parsing
- Some characters have special meaning (?=&)
- Different systems interpret characters differently
- URL encoding ensures compatibility
URL-Safe vs Unsafe Characters
Safe Characters
These characters are always safe in URLs:
Letters: A-Z, a-z Numbers: 0-9 Unreserved: - . _ ~ (no encoding needed)
Examples:
β https://example.com/my-article β https://example.com/blog_2025 β https://example.com/item.php
Reserved Characters
These have special meaning in URLs:
Character Meaning Example / Path separator https://example.com/path ? Query start https://example.com?param=value & Parameter sep ...?a=1&b=2 # Fragment start ...#section @ User info https://[email protected] : Port separator https://example.com:8080 = Parameter value ...?name=value
When to encode: When used literally in values
Unsafe/Special Characters
Must be encoded in URLs:
Character Encodes to Use Case Space %20 Between words ! %21 End of statement " %22 Quotes # %23 Heading references $ %24 Currency % %25 Percentage & %26 Parameter separator ' %27 Apostrophes ( %28 Grouping ) %29 Grouping + %2B Plus (addition) , %2C Comma / %2F Slashes : %3A Colons ; %3B Semicolons = %3D Equals ? %3F Question mark @ %40 At symbol [ %5B Brackets ] %5D Brackets ~ %7E Tilde
Common URL Encoding Examples
Example 1: Search Queries
Original: "best online tools" Encoded: "best%20online%20tools"
URL:
https://example.com/search?q=best%20online%20tools
Example 2: Email in URL
Original: [email protected] Encoded: user%40example.com
URL:
https://example.com/user/user%40example.com
Example 3: File Names
Original: "my document.pdf" Encoded: "my%20document.pdf"
URL:
https://example.com/downloads/my%20document.pdf
Example 4: Parameters with Special Chars
Original: price=$100&discount=50% Encoded: price=%24100&discount=50%25
URL:
https://shop.com?price=%24100&discount=50%25
URL Encoding vs HTML/JavaScript
Different Contexts, Different Rules
// URL Encoding (for URLs)
"hello world" β "hello%20world"
// HTML Entity Encoding (for HTML)
"hello world" β "hello world" (no change needed)
"<script>" β "<script>"
// JavaScript Encoding (for JavaScript)
"hello world" β "hello world" (no change)
"alert('hi')" β "alert(\'hi\')" (escape quotes)
When to Use Each
| Context | Encoding | Example |
|---|---|---|
| URL parameters | URL encode | ?name=hello%20world |
| HTML attributes | URL or HTML encode | <a href="...%20..."> |
| JavaScript strings | JavaScript escape | var x = "hello\nworld" |
| JSON data | JSON escape | {"name":"hello\"world"} |
URL Encoding Tools
Using an Online Encoder
Step 1: Enter your text
[email protected] 2025
Step 2: Choose operation
- Encode (add % codes)
- Decode (remove % codes)
Step 3: Get result
user%40example.com%202025
Programming Examples
JavaScript:
// Encode
encodeURIComponent('hello world!');
// Result: "hello%20world%21"
// Decode
decodeURIComponent('hello%20world%21');
// Result: "hello world!"
Python:
from urllib.parse import quote, unquote
# Encode
quote("hello world!")
# Result: "hello%20world%21"
# Decode
unquote("hello%20world%21")
# Result: "hello world!"
Practical Use Cases
Case 1: Building Search URLs
const searchTerm = 'best tools 2025';
const encodedTerm = encodeURIComponent(searchTerm);
const url = `https://google.com/search?q=${encodedTerm}`;
// Result: https://google.com/search?q=best%20tools%202025
Case 2: Creating Download Links
<!-- Wrong: spaces break the link --> <a href="/downloads/my document.pdf">Download</a> <!-- Right: encoded filename works --> <a href="/downloads/my%20document.pdf">Download</a>
Case 3: API Parameters
const params = {
email: '[email protected]',
message: 'Hello, World!',
amount: '$100',
};
const urlParams = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
urlParams.append(key, value);
});
const apiUrl = `https://api.example.com?${urlParams}`;
// Browser automatically URL-encodes values
Common Mistakes
Mistake 1: Double Encoding
// WRONG - Encodes twice
const encoded = encodeURIComponent(encodeURIComponent('hello world'));
// Result: "hello%2520world" (% itself got encoded!)
// RIGHT - Encode once
const encoded = encodeURIComponent('hello world');
// Result: "hello%20world"
Mistake 2: Not Encoding Query Parameters
// WRONG - Special chars break query const url = `https://[email protected]&name=John Doe`; // Problem: & and spaces cause issues // RIGHT - Encode parameter values const email = encodeURIComponent('[email protected]'); const name = encodeURIComponent('John Doe'); const url = `https://api.example.com?email=${email}&name=${name}`;
Mistake 3: Encoding the Whole URL
// WRONG - Encodes the slashes and colons!
encodeURIComponent('https://example.com/path');
// Result: "https%3A%2F%2Fexample.com%2Fpath"
// RIGHT - Only encode specific parts
const path = 'search results';
const encoded = encodeURIComponent(path);
const url = `https://example.com/search/${encoded}`;
Encoding Different Parts of a URL
URL Structure
https://user:[email protected]:8080/path/to/page?name=value&color=blue#section \_____/ \___/ \___/\__________/\__/ \_____________/ \___________________/ \_____/ protocol user pass domain port path query string fragment
What to Encode
| Part | Encoding | Example |
|---|---|---|
| Protocol | No | https:// (keep as-is) |
| Domain | No | example.com (keep as-is) |
| Path | Yes | /search/my%20query |
| Query string values | Yes | ?q=hello%20world |
| Fragment | Sometimes | #my%20section |
Frequently Asked Questions
Q: When should I URL encode? A: When special characters appear in URL components (parameters, paths, fragments).
Q: Is URL encoding the same as encryption? A: No. URL encoding is reversible and visible. Encryption protects data with a key.
Q: What if I encode something that's already encoded? A: Double encoding breaks the URL. Encode only at input, not repeatedly.
Q: Do I need to encode in HTTPS? A: Yes. HTTPS encrypts the data, but URL encoding is still necessary for valid URLs.
Q: Can browsers decode URLs automatically? A: Browsers decode %20 to space, %3F to ?, etc. But you should encode to be safe.
Tools and Resources
URL Encoder/Decoder - Quick encoding/decoding
Related Guides:
Updated: November 2025 | Reading time: 10 minutes