URL Encoding: When and How to Use It

A practical guide to URL encoding, covering when to use it, common mistakes, and best practices for web development.

What is URL Encoding?

URL encoding, also known as percent-encoding, is a method to convert characters into a format that can be safely transmitted over the Internet. It replaces unsafe ASCII characters with a '%' followed by two hexadecimal digits, ensuring that URLs remain valid and data is transmitted correctly across different systems and protocols.

Why URL Encoding is Necessary

URLs can only contain a limited set of ASCII characters. Any characters outside this set must be encoded:

  • Spaces and special characters
  • Non-ASCII characters (Unicode)
  • Reserved characters with special meaning in URLs
  • Unsafe characters that might be misinterpreted

Common Encoding Examples

Space: " " → %20
Plus: "+" → %2B
Ampersand: "&" → %26
Equals: "=" → %3D
Question Mark: "?" → %3F
Hash: "#" → %23
Forward Slash: "/" → %2F
Percent: "%" → %25

When to Use URL Encoding

1. Query Parameters

Always encode values in URL query strings to prevent breaking the URL structure:

// Before encoding
https://api.example.com/search?q=hello world&category=tech

// After encoding
https://api.example.com/search?q=hello%20world&category=tech

2. Path Segments

Encode special characters in URL path segments:

// Before encoding
https://example.com/users/john doe

// After encoding
https://example.com/users/john%20doe

3. Form Data

Form data submitted via GET requests needs proper encoding:

// User input: "email@example.com"
// Encoded: "email%40example.com"

Encoding Methods

encodeURIComponent()

Use for encoding individual URL components (query parameters, path segments):

const searchQuery = "hello world!";
const encoded = encodeURIComponent(searchQuery);
// Result: "hello%20world%21"

const url = `https://api.example.com/search?q=${encoded}`;

encodeURI()

Use for encoding complete URLs while preserving URL structure:

const url = "https://example.com/path with spaces/file.html";
const encoded = encodeURI(url);
// Result: "https://example.com/path%20with%20spaces/file.html"

Common Mistakes to Avoid

1. Double Encoding

Encoding the same value multiple times can cause issues:

// Wrong - double encoding
const value = "hello world";
const encoded = encodeURIComponent(encodeURIComponent(value));
// Result: "hello%2520world" (incorrect)

// Correct - single encoding
const encoded = encodeURIComponent(value);
// Result: "hello%20world"

2. Not Encoding User Input

Always encode user input before including it in URLs. Unencoded input can break URLs or cause security issues.

3. Using Wrong Method

Choose the appropriate encoding method:

  • encodeURIComponent: For query parameters and individual components
  • encodeURI: For complete URLs
  • Don't use: escape() - it's deprecated

4. Space Encoding Confusion

Spaces can be encoded as %20 or + in query strings. Be aware of how your server handles both:

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

// But in path segments, only %20 is valid
/path/hello%20world  ✓
/path/hello+world    ✗

Best Practices

  • Always encode user input: Never trust user input to be URL-safe
  • Use appropriate method: encodeURIComponent for parts, encodeURI for full URLs
  • Decode server-side: Most frameworks decode automatically, but verify
  • Handle Unicode properly: URL encoding uses UTF-8 for non-ASCII characters
  • Test edge cases: Test with special characters, spaces, and Unicode
  • Document encoding: Make it clear which parameters need encoding

Real-World Examples

Building Search URLs

function buildSearchUrl(query, filters) {
  const baseUrl = 'https://api.example.com/search';
  const params = new URLSearchParams({
    q: query,
    category: filters.category,
    sort: filters.sort
  });
  return `${baseUrl}?${params.toString()}`;
}

// Usage
const url = buildSearchUrl('hello world', {
  category: 'tech & science',
  sort: 'newest'
});
// Result: https://api.example.com/search?q=hello+world&category=tech+%26+science&sort=newest

Try Our URL Encoder

Practice URL encoding and decoding with our free online tool: