April 7, 2026 2 min readUpdated Apr 9, 2026
URL Encoding Explained: When and Why to Encode URLs
url-encodingweb-developmentjavascripthttpapi

What Is URL Encoding?
URL encoding, also called percent encoding, converts characters not allowed in a URL into a safe representation. Each unsafe character is replaced by % followed by its two-digit hexadecimal ASCII code.
Original: hello world & more
Encoded: hello%20world%20%26%20more
Why Is URL Encoding Necessary?
URLs can only contain characters defined by RFC 3986:
- Unreserved:
A–Z,a–z,0–9,-,_,.,~— never need encoding - Reserved:
:,/,?,#,&,=, etc. — have special meaning; encode if used as data - Everything else — must always be encoded
Without encoding, characters like ?, &, and = break query string parsing.
Encoding in JavaScript
// encodeURIComponent — use for individual query parameter values
encodeURIComponent('hello world & more');
// 'hello%20world%20%26%20more'
// encodeURI — use for a complete URL (preserves structure)
encodeURI('https://example.com/search?q=hello world');
// 'https://example.com/search?q=hello%20world'
// Decode
decodeURIComponent('hello%20world%20%26%20more'); // 'hello world & more'
Rule: Use encodeURIComponent for query parameter values. Use encodeURI for full URLs.
Encoding in Python
from urllib.parse import quote, urlencode
# Encode a single value
quote('hello world & more') # 'hello%20world%20%26%20more'
# Build a query string safely
params = {'q': 'hello world', 'lang': 'en'}
urlencode(params) # 'q=hello+world&lang=en'
Form Encoding vs Percent Encoding
- Percent encoding (RFC 3986): Spaces become
%20. Used in URL paths. - Form encoding: Spaces become
+. Used by HTML form submissions.
Common Pitfalls
- Double encoding: Encoding an already-encoded URL produces
%2520instead of%20. Always decode first. - Forgetting to encode user input: Always encode dynamic values you build into URLs.
const url = `https://api.example.com/search?q=${encodeURIComponent(userInput)}`;
Try It Instantly
Use the free URL Encoder/Decoder on konvertio.app — paste any string and get the encoded or decoded output instantly.