Back to Blog

URL Encoding Explained: How to Encode and Decode a URL Correctly

What URL encoding is, why URLs need it, and how to encode or decode special characters, spaces, and query parameters correctly.

September 6, 2026
Reading time: ~3 min

URL encoding (also called percent-encoding) converts characters that aren't safe in a URL — spaces, ampersands, question marks, non-English characters — into a % followed by a hex code, so the URL can be transmitted correctly without breaking. A space becomes %20, an ampersand becomes %26, and so on. A URL encoder does this conversion automatically; a URL decoder reverses it, turning %20 back into a readable space.

Why do URLs need encoding at all?

URLs can only safely contain a limited set of ASCII characters — letters, numbers, and a handful of symbols like -, _, ., and ~. Everything else has to be encoded, because certain characters already have a reserved meaning in a URL's structure:

  • ? starts the query string
  • & separates query parameters
  • # starts a URL fragment
  • / separates path segments
  • Spaces aren't allowed at all in a raw URL

If you put a literal space, &, or accented character into a URL without encoding it, the URL can break, truncate, or get misinterpreted by the server — sending the wrong data or none at all.

When do you actually need to encode a URL?

  • Passing search queries or text as a URL parameter — e.g., a search box that sends ?q=cats & dogs needs the & encoded to %26, or the server will read it as two separate parameters.
  • Handling user-generated content in links — names, addresses, or comments with apostrophes, accented letters, or symbols.
  • Building API requests where parameters are appended to a URL.
  • Sharing links with special characters — emoji, non-Latin scripts, or symbols in a URL path or query.

How do I encode a URL?

  1. Identify what needs encoding — usually the query string values (the part after ?), not the entire URL structure.
  2. Paste the text into a URL encoder. It converts every unsafe character to its percent-encoded equivalent automatically.
  3. Use the encoded output in your link, API call, or query parameter.
  4. Decode when needed — if you receive a URL with %20 or %3D in it and need to read the original text, a URL decoder reverses the process instantly.

Common encoded characters

CharacterEncoded
Space%20
&%26
?%3F
#%23
/%2F
=%3D

What's the difference between encodeURI and encodeURIComponent?

If you're a developer working in JavaScript, this distinction matters: encodeURI() encodes a full URL but leaves characters like /, ?, and & untouched, since those are assumed to be part of the URL's structure. encodeURIComponent() encodes everything, including those structural characters — which is what you want when encoding a single value (like a search term) that will be inserted into a URL, not the URL as a whole. Using the wrong one is a common source of broken links and mangled query parameters.

Is URL encoding the same as Base64 encoding?

No, and they solve different problems. URL encoding makes text safe to include in a URL. Base64 encoding converts binary data (or text) into a compact ASCII representation, often used for embedding data or encoding credentials — not specifically designed for URLs, though Base64 output sometimes still needs URL encoding on top if it contains characters like + or /. For a deeper look at how Base64 works, see What Is Base64 Encoding? A Practical Explanation for Non-Cryptographers.

The takeaway

URL encoding exists to keep URLs functional when they carry anything beyond plain ASCII text — spaces, symbols, or special characters. Rather than manually looking up percent-codes or debugging a broken link one character at a time, a URL encoder/decoder handles the conversion instantly in both directions, which is especially useful when building links with dynamic search terms, API parameters, or user input.

  • #developer-tools
  • #guides
  • #encoding
0 comments

    Last updated

    September 6, 2026

    Back to Blog