Skip to main content
Blog / Web Development

Understanding URL Encoding - Complete Guide to Percent Encoding

Learn everything about URL encoding (percent-encoding), how it works, when to use it, and best practices for encoding URLs and URI components safely.

DevToolsCenter Team
6 min read

What is URL Encoding?

URL encoding, also known as percent-encoding, is a mechanism for encoding information in a Uniform Resource Identifier (URI). It converts special characters and non-ASCII characters into a format that can be safely transmitted over the internet.

Why URL Encoding is Necessary

URLs can only contain a limited set of characters from the ASCII character set. Special characters, spaces, and non-ASCII characters must be encoded to:

  • Prevent Ambiguity: Avoid conflicts with URL syntax (e.g., &, =, ?)
  • Ensure Compatibility: Support international characters and Unicode
  • Maintain Integrity: Preserve data during transmission

How URL Encoding Works

URL encoding converts characters into a % followed by two hexadecimal digits representing the character's ASCII value.

Reserved Characters

These characters have special meaning in URLs and must be encoded:

!  *  '  (  )  ;  :  @  &  =  +  $  ,  /  ?  %  #  [  ]

Example Encoding

Original:  hello world & special!
Encoded:   hello%20world%20%26%20special%21

Breakdown:
  space ( ) → %20
  & → %26
  ! → %21

URL Encoding vs URI Component Encoding

encodeURI() vs encodeURIComponent()

encodeURI() - Encodes the entire URL, but preserves:

  • Special URL characters: :, /, ?, #, [, ], @, !, $, &, ', (, ), *, +, ,, ;, =
  • Only encodes characters that have meaning outside their reserved purpose

encodeURIComponent() - Encodes URI components (query string values):

  • Encodes everything except: A-Z, a-z, 0-9, -, _, ., !, ~, *, ', (, )
  • Use for encoding individual query parameters or path segments

Practical Example

// Full URL encoding (preserves URL structure)
const url = "https://example.com/search?q=hello world";
const encoded = encodeURI(url);
// Result: "https://example.com/search?q=hello%20world"

// Component encoding (for query values)
const query = "hello world & special!";
const encodedComponent = encodeURIComponent(query);
// Result: "hello%20world%20%26%20special%21"

Common Use Cases

1. Query Strings

When building URLs with query parameters:

const baseUrl = "https://api.example.com/search";
const query = "coffee & tea";
const params = new URLSearchParams({ q: query });
const url = `${baseUrl}?${params.toString()}`;
// Result: "https://api.example.com/search?q=coffee+%26+tea"

2. Path Segments

Encoding filenames and path components:

const filename = "report 2024.pdf";
const encoded = encodeURIComponent(filename);
const url = `https://example.com/files/${encoded}`;
// Result: "https://example.com/files/report%202024.pdf"

3. Form Data

Encoding form submissions:

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

const encoded = Object.entries(formData)
  .map(([key, value]) => 
    `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
  )
  .join('&');
// Result: "name=John+Doe&email=john%40example.com&message=Hello+%26+welcome%21"

4. API Endpoints

Building dynamic API URLs:

const userId = "user#123";
const endpoint = `/api/users/${encodeURIComponent(userId)}`;
// Result: "/api/users/user%23123"

URL Encoding Best Practices

When to Encode

Always encode:

  • Query parameter values
  • Path segments with special characters
  • User-generated content
  • International characters (non-ASCII)
  • Spaces and special characters

Don't encode:

  • The entire URL (use encodeURI() instead)
  • Already encoded strings (avoid double encoding)
  • Reserved URL structure characters when encoding full URLs

Common Mistakes

  1. Double Encoding: Encoding an already encoded string

    // Wrong
    const double = encodeURIComponent(encodeURIComponent("hello"));
    
    // Right
    const single = encodeURIComponent("hello");
  2. Encoding Full URLs: Using encodeURIComponent() on entire URLs

    // Wrong
    encodeURIComponent("https://example.com/page");
    
    // Right
    encodeURI("https://example.com/page");
  3. Not Encoding Spaces: Spaces should be encoded as %20 or + in query strings

Special Characters Reference

Character Encoded Description
Space %20 or + Space character
& %26 Ampersand
= %3D Equals sign
? %3F Question mark
# %23 Hash/Pound
% %25 Percent sign
+ %2B Plus sign
/ %2F Forward slash
: %3A Colon
@ %40 At symbol

Internationalization Tips

  • Non-ASCII characters (e.g., emojis, CJK) must be percent-encoded in URLs
  • Example: "café ☕" → encodeURIComponent("café ☕") → "caf%C3%A9%20%E2%98%95"

Server-side Pitfalls

  • Avoid double-encoding: many frameworks auto-encode query strings
  • Always decode once at the boundary; validate inputs before use
  • Normalize encodings (UTF-8) to avoid mismatches

JavaScript Methods

Built-in Methods

// Encode full URI (preserves structure)
encodeURI("https://example.com/path?q=hello world")
// → "https://example.com/path?q=hello%20world"

// Encode URI component (for query values)
encodeURIComponent("hello world & special!")
// → "hello%20world%20%26%20special%21"

// Decode
decodeURI("https://example.com/path?q=hello%20world")
// → "https://example.com/path?q=hello world"

decodeURIComponent("hello%20world%20%26%20special%21")
// → "hello world & special!"

URLSearchParams API

Modern approach for handling query strings:

const params = new URLSearchParams();
params.set('q', 'hello world');
params.set('category', 'tech & gadgets');
params.toString();
// → "q=hello+world&category=tech+%26+gadgets"

// Parse URL
const url = new URL("https://example.com/search?q=hello+world");
url.searchParams.get('q'); // → "hello world"

Conclusion

URL encoding is essential for safely transmitting data in URLs and ensuring compatibility across different systems and protocols. Understanding when to use encodeURI() vs encodeURIComponent(), and following best practices, will help you build robust web applications that handle special characters and international content correctly.

Whether you're building query strings, API endpoints, or handling user input, proper URL encoding ensures your applications work reliably across all browsers and systems.

Try URL Encoder / Decoder Now

Ready to put this into practice? Use our free URL Encoder / Decoder tool. It works entirely in your browser with no signup required.

Launch URL Encoder / Decoder
Free Forever No Signup Browser-Based

Frequently Asked Questions

Q Should I use encodeURI or encodeURIComponent?

A

Use encodeURI for full URLs and encodeURIComponent for individual components like query values.