FTJ
← Blog
Developer

URL Encoding: Why %20 Means Space and Other Mysteries

Ever wondered why some URLs have %20 and %3F in them? Here's what URL encoding does and a free tool to do it for you.

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

Why Do We Need URL Encoding?

URLs can only contain a limited set of ASCII characters. Characters outside this set must be encoded:

  • Spaces%20
  • Chinese/Japanese%E4%B8%AD%E6%96%87
  • Ampersands%26
  • Plus signs%2B

Common URL Encoded Characters

CharacterEncoded
space%20
!%21
#%23
$%24
&%26
/%2F
=%3D
?%3F
@%40

When to Encode

  • Query parameters: ?q=hello%20world
  • Path segments: /files/my%20document.pdf
  • API requests: Ensuring special characters don't break the request
  • Form data: Application/x-www-form-urlencoded format

When NOT to Encode

  • The URL scheme (http://, https://)
  • Host name (example.com)
  • Reserved characters that have structural meaning (:, /, ?, #, &, =)

## URL Encoding in Different Programming Languages

Most languages have built-in functions for URL encoding. Here is how to encode a string in popular languages:

  • JavaScript: encodeURIComponent("hello world") returns hello%20world
  • Python: urllib.parse.quote("hello world") returns hello%20world
  • PHP: urlencode("hello world") returns hello+world (spaces become +, not %20)
  • Go: url.QueryEscape("hello world") returns hello+world
  • Java: URLEncoder.encode("hello world", "UTF-8") returns hello+world
  • Ruby: URI.encode_www_form_component("hello world") returns hello+world

Note the difference: encodeURIComponent in JavaScript uses %20 for spaces, while urlencode in PHP and similar functions in other languages use +. This is because there are two encoding standards: one for URL paths (RFC 3986, uses %20) and one for form data (HTML specification, uses +).

Common Pitfalls

  1. Double encoding: Encoding an already-encoded string produces %2520 instead of %20. Always decode first if you are unsure.
  2. Forgetting to encode query parameters: A URL like ?name=John & Sons breaks because the ampersand is treated as a parameter separator. Always encode parameter values.
  3. Encoding the entire URL: encodeURIComponent("https://example.com") encodes the slashes and colon, breaking the URL. Use encodeURI for full URLs, encodeURIComponent for individual components.
  4. Mixing encoding standards: If your API expects %20 but you send +, some servers reject the request. Check the API documentation.

How Browsers Handle URL Encoding

When you type a URL with non-ASCII characters into the address bar, the browser automatically encodes it before sending the request. For example, if you paste https://example.com/search?q=café, the browser sends https://example.com/search?q=caf%C3%A9.

This is why copying a URL from the address bar sometimes looks different from what you typed. The displayed URL is decoded for readability, but the actual request uses the encoded form. Use our free URL encoder/decoder to encode or decode URLs instantly.

Try These Tools

More Articles