正在加载,请稍候…

URL Encoding Explained: Why Special Characters Must Be Escaped

Learn why URLs need encoding, what percent-encoding is, and how to encode/decode URLs correctly using our free online URL encoder and decoder tool.

What Is URL Encoding?

URL encoding (also called percent-encoding) converts characters that are not allowed or that have special meaning in URLs into a format that can be safely transmitted. The encoded form uses a percent sign (%) followed by the two-digit hexadecimal representation of the character's byte value.

Why URLs Need Encoding

URLs can only contain a limited set of characters from the ASCII character set. Characters outside this set, and certain reserved characters that have special meaning in URL syntax, must be encoded.

Reserved Characters

Some characters are "reserved" because they have special meaning in URL structure:

  • ?: Query string delimiter
  • &: Parameter separator
  • =: Key-value separator
  • #: Fragment identifier
  • /: Path separator
  • +: Space (in query strings)
  • @, :, !: Various protocol uses

When these characters appear as data (not as structural elements), they must be encoded.

Unsafe Characters

Characters that are not safe to transmit without encoding:

  • Spaces: Encoded as %20 (or + in query strings)
  • Special characters: <, >, ", {, }, |, , ^, `, [, ]
  • Non-ASCII characters: Any character outside the 128 ASCII characters

Encoding Examples

Original Encoded
Hello World Hello%20World
user@example.com user%40example.com
a+b=c a%2Bb%3Dc
café caf%C3%A9
你好 %E4%BD%A0%E5%A5%BD
emoji 😀 %F0%9F%98%80

URL Components and Their Rules

Different parts of a URL have different encoding rules:

Path Segments

Slashes (/) are path separators. Within a path segment, encode reserved characters but keep unreserved ones (letters, digits, -, _, ., ~).

Query Parameters

The key=value pairs after the ? separator. Both keys and values should be encoded. Historically, spaces can be + in query strings (application/x-www-form-urlencoded format).

Fragment Identifiers

The portion after #. Used for in-page navigation. Should be encoded but + does not represent space here.

JavaScript URL Encoding Functions

JavaScript provides several related functions:

encodeURIComponent()

Encodes a single URI component (query value, path segment). Encodes everything except: A-Z a-z 0-9 - _ . ! ~ * ' ( )

encodeURIComponent('Hello World!')  // 'Hello%20World!'
encodeURIComponent('user@example.com')  // 'user%40example.com'

encodeURI()

Encodes a complete URI, preserving structural characters. Does NOT encode: ; , / ? : @ & = + $ # A-Z a-z 0-9 - _ . ! ~ * ' ( )

encodeURI('https://example.com/path?q=hello world')
// 'https://example.com/path?q=hello%20world'

URLSearchParams

The modern API for query string handling:

const params = new URLSearchParams({
  query: 'hello world',
  filter: 'a+b=c'
});
console.log(params.toString());
// 'query=hello+world&filter=a%2Bb%3Dc'

IDN (Internationalized Domain Names)

Non-ASCII domain names (like münchen.de) are handled through Punycode encoding:

  • münchen.de → xn--mnchen-3ya.de

This allows Unicode in domain names while maintaining backward compatibility with DNS infrastructure.

Using the URL Encoder Tool

Our tool:

  1. Encode any text — converts to percent-encoded URL-safe format
  2. Decode encoded URLs — restore original text from encoded form
  3. Component vs. full URL mode — apply appropriate encoding rules
  4. Query parameter builder — build properly encoded query strings from key-value pairs
  5. Copy result — one-click copy of encoded/decoded output

Use it for building API URLs with special characters, debugging URL encoding issues, encoding user input for URL parameters, and understanding what percent-encoded URLs actually contain.