Skip to main content
Development

URL Encoding Explained: How to Encode and Decode URLs

Learn what URL encoding is, why special characters must be encoded in web addresses, and how percent-encoding works. Includes common encoded characters and free tools.

February 12, 202610 min readBy Tovlix Team

# URL Encoding Explained: How to Encode and Decode URLs


URLs can only contain a limited set of characters. When you need to include spaces, special characters, or non-ASCII text in a web address, they must be converted into a format the browser can understand. This process is called URL encoding (also known as percent-encoding). This guide explains how it works, when you need it, and the most common encoding patterns.


What Is URL Encoding?


URL encoding replaces unsafe or reserved characters with a percent sign (%) followed by their hexadecimal ASCII code. For example, a space becomes `%20`, and an ampersand (&) becomes `%26`.


Example:

  • Original: `Hello World & Friends`
  • Encoded: `Hello%20World%20%26%20Friends`

  • This ensures the URL is transmitted correctly across the internet without ambiguity.


    Why URL Encoding Is Necessary


    URLs have a strict syntax defined by RFC 3986. Certain characters have special meanings:


    CharacterMeaning in URLsEncoded Form
    /Path separator%2F
    ?Query string start%3F
    &Parameter separator%26
    =Key-value assignment%3D
    #Fragment identifier%23
    :Scheme separator (http:)%3A
    @User info separator%40
    +Space (in form data)%2B

    If you want to use any of these characters as literal data (not as structural elements), you must encode them. Otherwise, the browser interprets them as URL syntax.


    Example problem:

    You're searching for "C++ programming" on a website:

  • Wrong: `site.com/search?q=C++ programming`
  • The browser sees the `+` signs and spaces as syntax, breaking the URL
  • Correct: `site.com/search?q=C%2B%2B%20programming`

  • How Percent-Encoding Works


    The Algorithm


  • Take the character's byte value in UTF-8
  • Convert each byte to a two-digit hexadecimal number
  • Prepend a percent sign (%)

  • Example: The space character

  • ASCII value: 32
  • Hexadecimal: 20
  • Encoded: %20

  • Example: The emoji character (multi-byte UTF-8)

  • UTF-8 bytes: E2 9C 93
  • Encoded: %E2%9C%93

  • Characters That DON'T Need Encoding


    These characters are safe to use in URLs without encoding:


  • Letters: - A-Z, a-z
  • Digits: - 0-9
  • Unreserved characters: - _ . ~

  • Everything else should be encoded if used as data (not as URL structure).


    Common URL Encoding Reference


    CharacterNameEncoded
    (space)Space%20 or +
    !Exclamation%21
    "Double quote%22
    #Hash%23
    $Dollar%24
    %Percent%25
    &Ampersand%26
    'Single quote%27
    (Open parenthesis%28
    )Close parenthesis%29
    +Plus%2B
    ,Comma%2C
    /Forward slash%2F
    :Colon%3A
    ;Semicolon%3B
    =Equals%3D
    ?Question mark%3F
    @At sign%40
    [Open bracket%5B
    ]Close bracket%5D

    Space Encoding: %20 vs. +


    You'll see spaces encoded two ways:


  • %20 - — Standard URL encoding (RFC 3986). Used in URL paths
  • + - — Form encoding (application/x-www-form-urlencoded). Used in query strings from HTML forms

  • In practice:

  • `site.com/path/hello%20world` — Path uses %20
  • `site.com/search?q=hello+world` — Query string uses +

  • Both are valid in query strings, but %20 is the more universal encoding that works everywhere.


    URL Encoding in Programming


    JavaScript


    // Encode a URI component (query parameters, path segments)
    encodeURIComponent("Hello World & Friends")
    // Returns: "Hello%20World%20%26%20Friends"
    
    // Encode a full URI (preserves ://?#& structure)
    encodeURI("https://site.com/path?q=hello world")
    // Returns: "https://site.com/path?q=hello%20world"
    
    // Decode
    decodeURIComponent("Hello%20World%20%26%20Friends")
    // Returns: "Hello World & Friends"

    Important difference:

  • `encodeURIComponent()` encodes everything except letters, digits, and `- _ . ~`
  • `encodeURI()` preserves URL-structural characters like `: / ? # & =`

  • Use `encodeURIComponent()` for individual parameters. Use `encodeURI()` for complete URLs.


    Python


    from urllib.parse import quote, unquote, urlencode
    
    # Encode a string
    quote("Hello World & Friends")
    # Returns: "Hello%20World%20%26%20Friends"
    
    # Encode query parameters
    urlencode({"q": "Hello World", "lang": "en"})
    # Returns: "q=Hello+World&lang=en"
    
    # Decode
    unquote("Hello%20World%20%26%20Friends")
    # Returns: "Hello World & Friends"

    Common URL Encoding Mistakes


    1. Double Encoding


    Encoding an already-encoded string produces garbled results:

  • Original: `Hello World`
  • First encoding: `Hello%20World`
  • Double encoded: `Hello%2520World` (the % itself gets encoded)

  • Always check if data is already encoded before encoding it again.


    2. Not Encoding User Input


    When building URLs with user-supplied data, always encode the values:


    // Dangerous — user input is not encoded
    const url = `/search?q=${userInput}`;
    
    // Safe — user input is encoded
    const url = `/search?q=${encodeURIComponent(userInput)}`;

    Failing to encode user input can lead to broken URLs and security vulnerabilities like injection attacks.


    3. Encoding the Entire URL


    Don't encode the full URL including the protocol, domain, and path separators:

  • Wrong: `encodeURIComponent("https://site.com/path?q=test")`
  • - Result: `https%3A%2F%2Fsite.com%2Fpath%3Fq%3Dtest` (broken)

  • Right: `"https://site.com/path?q=" + encodeURIComponent("test value")`

  • Only encode the data portions, not the structural elements.


    4. Forgetting to Decode on the Server


    If you encode data in a URL, the receiving server must decode it. Most web frameworks handle this automatically, but custom URL parsing code might not.


    URL Encoding and Security


    Cross-Site Scripting (XSS) Prevention


    URL encoding is part of preventing XSS attacks. If user input is included in URLs without encoding, attackers can inject malicious scripts:


  • Malicious input: `<script>alert('hacked')</script>`
  • Without encoding: The script executes in the browser
  • With encoding: `%3Cscript%3Ealert(%27hacked%27)%3C%2Fscript%3E` — renders as harmless text

  • Always encode user input in URLs, and always decode and sanitize on the server side.


    SQL Injection via URLs


    Similar to XSS, unencoded URL parameters can carry SQL injection payloads. URL encoding is one layer of defense (but not the only one — always use parameterized queries).


    Free URL and Data Tools


    Work with URLs and data encoding with these free Tovlix tools:


  • URL Encoder/Decoder - Encode and decode URLs instantly
  • Base64 Encoder - Encode data in Base64 format
  • HTML Encoder - Encode HTML entities
  • JSON Formatter - Format and validate JSON
  • Hash Generator - Generate hash values for data integrity
  • QR Code Generator - Create QR codes from any URL

  • Conclusion


    URL encoding ensures that special characters in web addresses are transmitted correctly. Use `%20` for spaces, encode all user-supplied data in URLs, and be careful not to double-encode. In JavaScript, use `encodeURIComponent()` for individual values and `encodeURI()` for full URLs. URL encoding is also a security essential — it helps prevent XSS and injection attacks. Use our free URL Encoder/Decoder for quick encoding and decoding without writing code.


    url encodingpercent encodingweb developmentdeveloper toolsjavascriptsecurityurls

    Try Our Free Tools

    Generate passwords, QR codes, invoices, and 200+ more tools - completely free!

    Explore All Tools