URL Encoder: How URL Encoding Works and When You Need It

Quick Answer

URLs can only contain a limited set of characters. Spaces, accented characters, and symbols like &, #, and % all have special meanings or are outright invalid in URLs. URL encoding (also called percent-encoding) converts these characters into a format…

URLs can only contain a limited set of characters. Spaces, accented characters, and symbols like &, #, and % all have special meanings or are outright invalid in URLs. URL encoding (also called percent-encoding) converts these characters into a format that can be safely transmitted in a URL. This guide explains how it works and when you need it.

What Is URL Encoding?

URL encoding replaces unsafe characters with a percent sign (%) followed by two hexadecimal digits representing the character’s ASCII value. A space becomes %20, a # becomes %23, and an ampersand becomes %26.

Use our URL encoder/decoder tool to instantly encode or decode any URL or query string.

Reserved vs. Unreserved Characters

URL encoding distinguishes between reserved and unreserved characters. Unreserved characters (A-Z, a-z, 0-9, -, _, ., ~) can appear in URLs without encoding. Reserved characters (; / ? : @ & = + $ ,) have special structural meanings in URLs and must be encoded when used as data (not structure).

encodeURI vs. encodeURIComponent in JavaScript

JavaScript has two encoding functions that confuse many developers:

  • encodeURI(): Encodes a complete URL, leaving structural characters like /, ?, and & intact. Use this for full URLs.
  • encodeURIComponent(): Encodes everything except letters, digits, and -_.!~*'(). This encodes structural characters too. Use this for individual query parameter values.

Common URL Encoding Scenarios

  • Search queries: “new york city” becomes new%20york%20city or new+york+city
  • File paths: A file named “my document.pdf” needs encoding as my%20document.pdf
  • API parameters: Dates like “2026-04-08T09:30:00” need the colons encoded for some APIs
  • Internationalization: Non-ASCII characters like ü or 中 need UTF-8 encoding before percent-encoding

URL Encoding in Different Languages

Python: from urllib.parse import quote; quote(string, safe='')

PHP: urlencode($string) or rawurlencode($string)

Java: URLEncoder.encode(string, StandardCharsets.UTF_8)

Conclusion

URL encoding is one of those ubiquitous web concepts that quietly underpins almost everything that happens in a browser. Mastering the distinction between encoding a full URL versus encoding a parameter value will save you from subtle bugs that are notoriously difficult to track down.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top