Link Tools Dereferer Hide Referrer Link URL Shortener Affiliate Cloaker PayPal Links Privacy Tools Password Generator Cloudflare Resolver My Referrer Torrent Tools Magnet → Torrent Torrent → Magnet Torrent Editor Pirate Bay Proxies Movierulz Proxies ExtraTorrent Proxies Dev Tools Base64 Encoder Hash Generator HTTP Headers Disposable Email Checker Company Blog About Us Contact Anonymize Free
General

URL Encoding Explained: When and How to Encode URLs Correctly

JAY
Author
May 14, 2026 ·2 min read ·0 views
URL Encoding Explained: When and How to Encode URLs Correctly

URL encoding mistakes break APIs, cause 404 errors, and corrupt data. This guide explains exactly when and how to encode URLs correctly with real code examples.

What Is URL Encoding?

URL encoding (also called percent encoding) converts characters that are not allowed in URLs into a safe format using a % sign followed by two hexadecimal digits. For example, a space becomes %20, an ampersand becomes %26, and a forward slash becomes %2F.

Encode and decode URLs instantly: URL Encoder & Decoder

Why URLs Need Encoding

URLs can only contain a limited set of ASCII characters. The RFC 3986 specification defines which characters are "unreserved" (safe without encoding): A-Z a-z 0-9 - _ . ~

All other characters must be percent-encoded before being included in a URL. This includes spaces, special characters, non-ASCII characters (like accented letters and Chinese characters), and URL-reserved characters when used as data rather than delimiters.

encodeURI vs encodeURIComponent

FunctionWhat it encodesWhat it leavesUse for
encodeURI()Spaces and most special chars: / ? # [ ] @ ! $ & , ; = (URL structure)Complete URLs
encodeURIComponent()Everything except A-Z a-z 0-9 - _ . ! ~ * ( )Only alphanumerics and safe charsQuery parameter values

Code Examples

Correct — encoding a query parameter value:
var search = encodeURIComponent(userInput);
var url = "https://api.example.com/search?q=" + search;

Wrong — using encodeURI for a parameter value:
// encodeURI leaves & and = intact — breaks query strings
var url = "https://api.example.com?q=" + encodeURI("bread & butter");
// Result: ?q=bread%20&%20butter — & breaks the query string

Common URL Encoding Mistakes

1. Double Encoding

Encoding an already-encoded string produces %2520 instead of %20 (encoding the % sign). Always decode before re-encoding.

2. Not Encoding Query Values

Passing user input directly into query strings without encoding allows injection attacks and breaks APIs when input contains &, =, or #.

3. Using the Wrong Function

Using encodeURI instead of encodeURIComponent for individual query parameter values leaves characters like & and = unencoded, which breaks the query string structure.

URL Encoding Character Reference

CharacterEncodedNotes
Space%20 or ++ only valid in query strings, not paths
&%26Used to separate query params
=%3DUsed between key and value
#%23Fragment identifier
+%2B+ in query strings means space
/%2FPath separator
?%3FQuery string start
@%40Email and auth

Related Tools

# General
Share on X
Rate this article
Your rating is stored anonymously. You can rate once per post.
Written by
JAY
Writer at Anonymiz

Related Articles

Schema Markup Complete Guide 2026: Rich Snippets for SEO
May 14, 2026 · JAY
GDPR Checklist for Developers 2026: Everything You Need to Be Compliant
May 14, 2026 · JAY
The Complete HTTP Security Headers Guide 2026
May 14, 2026 · JAY
← Back to Blog
Done!