πŸ’» Text & Dev

πŸ”— πŸ”— URL Encoder / Decoder: How URL Encoding Works

Learn how URL encoding (percent-encoding) works and why it's needed. Covers reserved characters, the encoding process, query string encoding, and common URL encoding mistakes.

⏱️ 7 min read🦉 365tool.net🌍 For everyone worldwide

URLs can only contain a limited set of safe characters β€” letters, digits, and a few special characters. Everything else must be encoded using a system called percent-encoding (also called URL encoding). Understanding this encoding is essential for web developers building APIs, creating query parameters, working with web scraping, and debugging network requests.

Why URL Encoding Exists

URLs were originally defined in RFC 1738 (1994) to use only ASCII characters, and even within ASCII, many characters have special meaning in a URL's structure:

  • ? starts the query string
  • & separates query parameters
  • = separates a parameter name from its value
  • / separates URL path segments
  • # starts the fragment identifier

If these characters appear in data (not as structural delimiters), they must be encoded to prevent the URL parser from misinterpreting them. For example, a search query containing "&" would break query string parsing if not encoded.

How Percent-Encoding Works

Each unsafe character is replaced with %XX, where XX is the hexadecimal representation of the character's UTF-8 byte value.

Process:

  1. Convert the character to its UTF-8 byte sequence
  2. Express each byte as two hexadecimal digits
  3. Prefix each byte with a percent sign %

Examples

Character URL Encoded Why it needs encoding
Space%20Not allowed in URLs
&%26Reserved: query param separator
=%3DReserved: key=value separator
+%2BMeans "space" in query strings
#%23Reserved: fragment identifier
/%2FReserved: path separator
?%3FReserved: query start
Γ© (U+00E9)%C3%A9Non-ASCII; 2 UTF-8 bytes: C3 A9
πŸ˜€ (emoji)%F0%9F%98%804 UTF-8 bytes: F0 9F 98 80

Safe Characters That Don't Need Encoding

Per RFC 3986, these characters are "unreserved" and safe anywhere in a URL without encoding:

  • Letters: A–Z, a–z
  • Digits: 0–9
  • Hyphen: -
  • Underscore: _
  • Period: .
  • Tilde: ~

Space Encoding: %20 vs +

Space is encoded as %20 in URL paths and as either %20 or + in query strings. The + for space is a legacy from HTML form encoding (application/x-www-form-urlencoded) and is only valid in query string values, not in URL paths.

When decoding URLs, a decoder must know which context it's in β€” decoding + as a space in a URL path is incorrect.

Best practice: always use %20 for spaces (it works everywhere); use + only when targeting older form-based systems that specifically require it.

encodeURIComponent vs encodeURI in JavaScript

JavaScript provides two built-in encoding functions with different behavior:

  • encodeURI(url): Encodes a full URL. Does NOT encode: ; , / ? : @ & = + $ # (structural characters that should remain as-is in a complete URL). Use for encoding a full URL.
  • encodeURIComponent(value): Encodes a URL component (a parameter value). Encodes everything except unreserved characters. Use for encoding individual query parameter values.

Example: Encoding a search query "coffee & cake" as a URL parameter:

encodeURIComponent("coffee & cake") β†’ coffee%20%26%20cake

Full URL: https://example.com/search?q=coffee%20%26%20cake

Common URL Encoding Mistakes

  • Double-encoding: Encoding an already-encoded string encodes the % signs themselves. %20 becomes %2520 (incorrect). Always decode first if you're unsure whether input is already encoded.
  • Not encoding query values: Passing raw user input as query parameters breaks when the value contains &, =, or #.
  • Using + for space in paths: Only valid in query strings; breaks path resolution.
  • Forgetting non-ASCII characters: Characters like Γ±, ΓΌ, Chinese characters, and emoji must be UTF-8 encoded then percent-encoded.

Try It Yourself! ✨

Use our free URL Encoder / Decoder — results appear as you type. No sign-up needed!

🚀 Open URL Encoder / Decoder Free

❓ Frequently Asked Questions

What is URL encoding (percent-encoding)?
URL encoding converts characters that aren't safe in URLs into a percent (%) followed by two hexadecimal digits representing the character's UTF-8 byte value. A space becomes %20, an ampersand becomes %26, and an emoji like πŸ˜€ becomes %F0%9F%98%80. This ensures URLs only contain the limited set of characters that URL parsers can safely handle.
When do I need to URL encode something?
Encode any user-provided data placed in a URL β€” especially query parameter values. If someone searches for "coffee & cake" and you put it in a URL as ?q=coffee & cake, the & breaks the query string. The correct form is ?q=coffee%20%26%20cake. Always encode parameter values with encodeURIComponent() in JavaScript.
What is the difference between %20 and + for spaces?
%20 always means a space and works everywhere in a URL. The + sign means space only in query string values (application/x-www-form-urlencoded format, used in HTML form submissions). In URL paths, + is a literal plus character, not a space. Best practice: use %20 universally to avoid context-dependent decoding errors.
What is the difference between encodeURI and encodeURIComponent?
encodeURI() encodes a complete URL, preserving structural characters like ?, &, =, /, #. Use it when encoding a full URL. encodeURIComponent() encodes everything except unreserved characters (letters, digits, -, _, ., ~), making it safe for query parameter values where &, =, and + would be misinterpreted as structure. Use it for individual values placed in URLs.
What is double-encoding and how do I avoid it?
Double-encoding happens when you encode an already-encoded string. %20 (space) becomes %2520 (%25 is the encoding of %, followed by 20). This makes URLs malfunction β€” the server receives %20 as a literal string instead of a space. To avoid it: never encode URLs that are already encoded. If input might be encoded, decode it first (decodeURIComponent), then re-encode cleanly.