URL Encoding Explained: When and Why to Encode URLs
URLs can only contain certain ASCII characters. Learn what URL encoding is, when you need it, and how special characters like spaces and Chinese text get converted to percent-encoded strings.
Why URL Encoding Exists
Every URL you type into a browser follows strict rules defined in RFC 3986. Only a limited set of ASCII characters — letters, digits, and a handful of symbols like -, _, ., and ~ — are considered "safe" to use directly. Everything else — spaces, Chinese characters, accented letters, symbols like &, =, #, and ? — must be converted into a portable, text-safe representation.
URL encoding (formally called percent-encoding) solves this problem by replacing each unsafe byte with a % sign followed by two hexadecimal digits. For example, a space becomes %20, and the Chinese character 你 becomes %E4%BD%A0 (its UTF-8 byte sequence).
How URL Encoding Works
- Take the original character:
hello world - Identify unsafe characters: the space between "hello" and "world"
- Replace each unsafe byte with
%HH(percent + two hex digits):hello%20world - The result is a valid, portable URL component
Characters That Must Be Encoded
| Character | Encoded Form | Reason |
|---|---|---|
| Space | %20 | Not allowed in URLs |
| < > | %3C %3E | Delimiters in HTML |
| # | %23 | Fragment identifier |
| & | %26 | Query string separator |
| = | %3D | Key-value separator |
| Chinese / Unicode | %E4%BD%A0 | Non-ASCII bytes (UTF-8) |
When You Need URL Encoding
- Query parameters: Values containing spaces, symbols, or non-English text must be encoded (e.g.,
?q=hello%20world). - Form submissions: Browsers automatically encode form data as
application/x-www-form-urlencodedwhen submitting via GET or POST. - API requests: When building URLs programmatically, always encode dynamic values to avoid broken requests.
- File paths with spaces: A file named
my report.pdfbecomesmy%20report.pdfin a URL.
Step-by-Step: Encode or Decode a URL
- Open the URL encoder/decoder tool
- Paste your text (to encode) or percent-encoded string (to decode)
- Click "Encode" or "Decode"
- Copy the result instantly — no data leaves your browser
Common URL Encoding Pitfalls
- Double encoding: Encoding an already-encoded string turns
%20into%2520. This is a frequent source of bugs. - + vs %20: HTML forms use
+for spaces, but RFC 3986 uses%20. Know which convention your system expects. - Encoding reserved characters unnecessarily: Encoding characters that have structural meaning (like
/in paths) can break your URL. - Character encoding mismatch: Always encode using UTF-8. Using a different charset (like GBK or ISO-8859-1) will produce different bytes for the same character.
Try it now: Encode/Decode URLs →
Instant encoding and decoding. Works entirely in your browser — no server round-trip.
Frequently Asked Questions
What is URL encoding?
URL encoding (also called percent encoding) is a mechanism for converting characters that are not allowed in a URL into a portable format. Each unsafe character is replaced with a percent sign (%) followed by two hexadecimal digits representing the character's ASCII byte value. For example, a space becomes %20.
Why do URLs need to be encoded?
URLs can only contain a limited set of ASCII characters (letters, digits, and a few special symbols). Characters like spaces, Chinese characters, accented letters, and symbols such as & or = have special meanings or are invalid in URLs. Encoding converts these characters into a safe format so browsers and servers can transmit and interpret them correctly.
What is the difference between URL encoding and URL escaping?
They are the same thing. URL encoding and URL escaping both refer to the process of replacing unsafe characters with percent-encoded equivalents. The formal name defined in RFC 3986 is "percent-encoding," but developers commonly say URL encoding or URL escaping.
Do I need to encode spaces in a URL?
Yes. Spaces are not allowed in URLs. They should be encoded as %20 in the path or query string. Some older systems use the plus sign (+) for spaces specifically in query strings (application/x-www-form-urlencoded), but %20 is the standard per RFC 3986.