What is URL encoding?

URL encoding (also called percent encoding) converts special characters into a format that can be safely transmitted in URLs.

How URL Encoding Works:

Characters are encoded as a percent sign (%) followed by two hexadecimal digits:

  • Space becomes %20 or + (in query strings)
  • ! becomes %21
  • @ becomes %40
  • # becomes %23
  • UTF-8 characters like é become %C3%A9

Why URL Encoding is Necessary:

URLs can only safely contain certain characters:

  • Letters: A-Z, a-z
  • Digits: 0-9
  • Unreserved characters: - _ . ~
  • Reserved characters (when used for their purpose): : / ? # [ ] @ ! $ & ' ( ) * + , ; =*_

Any other characters must be encoded.

Common Use Cases:

  • Query parameters: ?name=John Doe?name=John%20Doe
  • Search queries: ?q=c++ programming?q=c%2B%2B%20programming
  • Special characters: ?email=user@example.com?email=user%40example.com
  • Unicode text: ?city=São Paulo?city=S%C3%A3o%20Paulo
  • Path segments: /files/my resume.pdf/files/my%20resume.pdf

Reserved Characters:

These have special meaning in URLs:

  • : separates protocol and domain
  • / separates path segments
  • ? starts query string
  • # starts fragment
  • & separates query parameters
  • = separates parameter name and value

When you need these characters as data (not structure), they must be encoded.

Example:

Original: Hello World! How are you? Encoded: Hello%20World%21%20How%20are%20you%3F

Important Notes:

  • URL encoding is reversible (anyone can decode it)
  • It's NOT encryption or security
  • Different parts of URLs have different encoding rules
  • Always encode user input before putting it in URLs
  • Modern browsers may show decoded URLs in the address bar

Related Questions