Skip to content
Web Development2026-06-023 min read

URL Encoding Explained: When and Why to Encode URLs

What is URL Encoding?

URL encoding (also called percent-encoding) converts characters into a format that can be safely transmitted over the internet. URLs can only contain a limited set of characters — ASCII letters, digits, and a few special characters like -, _, ., and ~.

Any character outside this set must be encoded using a % followed by two hexadecimal digits.

Why URL Encoding Matters

Reserved Characters

Some characters have special meaning in URLs:

| Character | Purpose | Encoded | |-----------|---------|---------| | ? | Query string start | %3F | | & | Parameter separator | %26 | | = | Key-value separator | %3D | | # | Fragment identifier | %23 | | / | Path separator | %2F | | : | Scheme/port separator | %3A |

If you need these characters as data (not syntax), they must be encoded.

Unsafe Characters

Some characters are always unsafe in URLs:

| Character | Reason | Encoded | |-----------|--------|---------| | Space | Can be modified by transport | %20 | | < > | Can be misinterpreted | %3C %3E | | { } | Reserved for future use | %7B %7D | | | \ | Gateways may modify | %7C %5C | | ^ [ ] | Unsafe in some systems | %5E %5B %5D |

Try It: URL Encoder/Decoder

Use our URL Encoder to encode and decode URLs instantly.

Common URL Encoding Examples

Spaces in Search Queries

Original: https://example.com/search?q=hello world
Encoded:  https://example.com/search?q=hello%20world

Special Characters in Parameters

Original: https://example.com/api?name=John&role=admin&bio=loves coding
Encoded:  https://example.com/api?name=John%26role%3Dadmin%26bio%3Dloves%20coding

Non-ASCII Characters (Unicode)

Original: https://example.com/search?q=café
Encoded:  https://example.com/search?q=caf%C3%A9

URL Encoding in Programming Languages

JavaScript

// Encode a full URL
encodeURI("https://example.com/path with spaces");
// "https://example.com/path%20with%20spaces"

// Encode a URL component (more aggressive)
encodeURIComponent("hello world&foo=bar");
// "hello%20world%26foo%3Dbar"

// Decode
decodeURIComponent("hello%20world");
// "hello world"

Python

from urllib.parse import quote, unquote, urlencode

# Encode a string
quote("hello world")  # "hello%20world"

# Encode query parameters
urlencode({"q": "hello world", "page": 1})
# "q=hello+world&page=1"

URL Encoding vs. HTML Entity Encoding

URL encoding and HTML entity encoding serve different purposes:

  • URL encoding: Makes text safe for URLs (&%26)
  • HTML entity encoding: Makes text safe for HTML (&&amp;)

Use our HTML Entities Encoder when working with HTML content.

Related Encoding Tools

Best Practices

  1. Always encode user input: Never trust raw user input in URLs
  2. Use encodeURIComponent for query parameter values in JavaScript
  3. Use server-side encoding: Your backend framework should handle encoding automatically
  4. Don't double-encode: Encoding an already-encoded string causes bugs
  5. Test edge cases: Use our URL Encoder to verify encoding

Conclusion

URL encoding is fundamental to web development. Use our URL Encoder/Decoder to quickly encode and decode URL components during development.

Try our free developer tools

All tools run in your browser with zero data uploads.

← Back to Blog