URL Encoding Explained: Special Characters and How to Handle Them
📅 May 25, 2026⏱️ 7 min read🔗 Network Tools
Every character in a URL has a meaning. Spaces, ampersands, question marks, and non-ASCII characters must be encoded or they'll break your links. This guide explains how URL encoding works and when to use it.
## Why URLs Need Encoding
URLs were designed to be read by computers, not humans. A URL like `https://example.com/search?q=hello world` contains a space — which is technically illegal in URLs. Browsers display it for convenience, but the actual encoded URL is `https://example.com/search?q=hello%20world`.
The `%20` is URL encoding. Every character outside the "safe" set gets replaced with `%XX` where XX is its hexadecimal ASCII value.
## The Safe Character Set
These characters don't need encoding in URLs:
```
A-Z a-z 0-9 - _ . ~
plaintext
Everything else — spaces, punctuation, non-ASCII characters, special symbols — must be percent-encoded.
## Common Encodings You Should Know
```
space → %20 ! → %21 " → %22 # → %23
$ → %24 % → %25 & → %26 ' → %27 ( → %28
) → %29 * → %2A + → %2B , → %2C / → %2F
: → %3A ; → %3B = → %3D ? → %3F @ → %40
[ → %5B ] → %5D { → %7B } → %7C | → %7C
## Query Parameters vs Path Segments
Different parts of a URL have different encoding rules:
- **Path segments** (e.g., `/blog/my post`) — encode everything except unreserved characters
- **Query string keys and values** (e.g., `?q=hello`) — encode using application/x-www-form-urlencoded
- **Query string separators** — `?`, `&`, and `=` are reserved and should not be encoded when they're serving as separators
## Common Mistakes to Avoid
### Mistake 1: Double Encoding
If a parameter value already contains encoded data, encode it again and you'll get double-encoding:
```
User's input: "hello%20world"
Wrong (double encode): "hello%2520world"
Right (single encode): "hello%20world"
plaintext
Use your framework's built-in URL encoding functions rather than manual string replacement.
### Mistake 2: Encoding Already Encoded URLs
Never encode a full URL — only encode the dynamic parts (query values, path segments with user content). The protocol, host, and structural characters must remain unencoded.
### Mistake 3: Forgetting Non-ASCII Characters
Chinese characters, emoji, accented letters — all must be UTF-8 encoded then percent-encoded:
```
"你好" → UTF-8 bytes → %E4%BD%A0%E5%A5%BD
JavaScript's `encodeURIComponent()` handles this correctly. Plain `encodeURI()` does not encode most non-ASCII characters.
## URL Encoding in Different Languages
```
JavaScript: encodeURIComponent(str) // for query values
encodeURI(str) // for full URLs
Python: urllib.parse.quote(s) // RFC 3986
urllib.parse.urlencode(dict) // query string
Node.js: encodeURIComponent(str) // same as JS
qs.stringify(obj) // for query objects
plaintext
## Base64URL — URL-Safe Base64
Standard Base64 uses `+` and `/`, which are unsafe in URLs. Base64URL replaces these:
```
Base64: + → + / → / = (padding)
Base64URL: + → - / → _ = (removed)
Used in JWT tokens and URL-safe data transmission.
## Try It Yourself
Use our [URL Encoder tool](../../tools/dev/url-encoder.html) to encode or decode any URL component. The tool automatically handles special characters, query strings, and non-ASCII text.
SOCIAL SHARE CARD GENERATOR