URL Encode

Percent-encode URLs and query strings — convert special characters to %XX format for safe URL transmission.

What is URL Encoding?

URL encoding (also called percent-encoding) is a mechanism for encoding special characters in a URL by replacing them with a percent sign (%) followed by two hexadecimal digits representing the character's byte value. For example, a space becomes %20, # becomes %23, and & becomes %26.

URLs can only contain a limited set of safe characters defined by RFC 3986. Any character outside this set — including spaces, Unicode characters, most punctuation, and all non-ASCII characters — must be percent-encoded before being included in a URL. This ensures the URL is valid and can be correctly parsed by web browsers, servers, and HTTP clients.

There are three common encoding modes: encodeURIComponent encodes everything except A–Z, a–z, 0–9, -, _, ., !, ~, *, ', (, ) — ideal for encoding individual query parameter values. encodeURI preserves the URL structure characters (/, :, ?, #, &, =) and only encodes non-URL characters — used for encoding a complete URL. Form encoding (application/x-www-form-urlencoded) replaces spaces with + instead of %20 — used in HTML form submissions.

Common use cases include encoding search queries in URL parameters, encoding file paths for web APIs, building dynamic URLs in JavaScript, and preparing query strings for HTTP requests.

Frequently Asked Questions

What's the difference between %20 and + for spaces?
%20 is the standard percent-encoding for a space in any URL context. The + sign is only used for spaces in the application/x-www-form-urlencoded format (HTML form submissions). In the query string of a URL, + is technically only valid as a space in form-encoded data, not in general URLs. When in doubt, use %20.
Should I use encodeURI or encodeURIComponent?
Use encodeURIComponent when encoding a single query parameter value (e.g., the user's search term). Use encodeURI when encoding an entire URL — it preserves the structural characters like /, ?, #, and & that give a URL its meaning.
Does URL encoding affect the domain name?
Standard percent-encoding does not apply to domain names. International domain names (IDN) with non-ASCII characters use Punycode encoding instead (e.g., münchen.de becomes xn--mnchen-3ya.de). Only the path and query string components use percent-encoding.
Why do some characters not get encoded?
The "unreserved characters" A–Z, a–z, 0–9, -, _, ., and ~ are always safe in URLs and are never percent-encoded. These characters have the same meaning in URLs and as literal characters, so encoding them would be unnecessary.