link Developer Tools

How to Encode and Decode URLs - Complete Guide with Examples

Learn how to encode and decode URLs properly. Step-by-step guide with URL encoding formulas, percent-encoding examples, and best practices for web development.

Ready to try it?

Use our free URL Encoder & Decoder now — no signup required.

open_in_new Open Tool

What is URL Encoding?

URL encoding is a method to convert special characters and non-ASCII characters into a safe format that can be transmitted over the internet. When URLs contain spaces, accented characters, or reserved symbols like &, =, or ?, these must be encoded to ensure proper transmission between web browsers and servers.

The encoding process replaces unsafe characters with a percent sign (%) followed by two hexadecimal digits representing the byte value in UTF-8 encoding. For example, a space becomes %20, and the ampersand (&) becomes %26. This process ensures that URLs remain valid and can be correctly interpreted by web servers.

URL decoding is the reverse process, converting percent-encoded characters back to their original form. This is essential when processing form data, query parameters, or any user input that was encoded before being included in a URL.

Formula and Methodology

The URL encoding formula follows the percent-encoding standard (RFC 3986):

Encoding Formula: Replace each unsafe character with % + 2-digit hex code

Step-by-step methodology:

  1. Identify characters that need encoding: spaces, special characters (<>#%{}|\^~[]`), non-ASCII characters (accents, emojis, CJK characters), and reserved characters (&;=+?$@!)
  2. Convert the character to its UTF-8 byte representation
  3. Convert each byte to a 2-digit hexadecimal number
  4. Prepend each hex value with %

Example conversions:

  • Space (ASCII 32) → 0x20 → %20
  • Ampersand (ASCII 38) → 0x26 → %26
  • Equal sign (ASCII 61) → 0x3D → %3D
  • Umlaut 'ü' (UTF-8: C3 BC) → %C3%BC
  • Emoji '😀' (UTF-8: F0 9F 98 80) → %F0%9F%98%80

Real-World Examples

Example 1: Simple query parameter with spaces

Original URL: https://example.com/search?q=hello world

Encoded: https://example.com/search?q=hello%20world

Step-by-step: The space character (ASCII 32 = 0x20) is replaced with %20

Example 2: URL with special characters

Original URL: https://example.com/api?name=John&age=30&city=New York

Encoded: https://example.com/api?name=John%26age%3D30%26city%3DNew%20York

Step-by-step: & (ASCII 38 = 0x26) → %26, = (ASCII 61 = 0x3D) → %3D, space → %20

Example 3: International characters

Original: https://example.com/greet?message=Hello Café

Encoded: https://example.com/greet?message=Hello%20Caf%C3%A9

Step-by-step: 'é' in UTF-8 is bytes C3 A9, so it becomes %C3%A9

Common Mistakes to Avoid

1. Double encoding: Encoding an already encoded URL. For example, encoding %20 again produces %2520. Always check if your data is already encoded before applying encoding.

2. Encoding the entire URL: Only encode the query parameters and path segments, not the protocol, domain, or reserved characters that have structural meaning. Encoding the entire URL like https://example.com to %68%74%74%70%3A%2F%2F... breaks the URL structure.

3. Incorrect character set: Always use UTF-8 encoding. Using legacy encodings like ISO-8859-1 can cause mojibake (garbled text) when handling international characters.

4. Encoding reserved characters unnecessarily: Characters like / in path segments or ? in the query string separator should not be encoded unless they are part of the actual data value.

5. Forgetting to decode server-side: When receiving URL-encoded data on the server, remember to decode it before processing. Failing to decode means your database will store %20 instead of spaces.

Step-by-Step Guide

  1. 1

    Gather Your Data

    Collect the URL or string that needs encoding or decoding. Identify whether you need to encode (convert to safe format) or decode (convert back to readable format).

  2. 2

    Enter Your Values

    Input your URL or string into the encoder/decoder tool. For encoding, paste the raw URL with spaces and special characters. For decoding, paste the percent-encoded URL.

  3. 3

    Calculate

    Click the Encode or Decode button. The tool will process each character: encoding converts unsafe characters to %XX format, while decoding converts %XX sequences back to original characters.

  4. 4

    Interpret Results

    Review the output. For encoded results, verify that spaces became %20, special characters got proper percent-encoding, and international characters show multi-byte sequences like %C3%A9 for é.

  5. 5

    Take Action

    Use the encoded URL in your web requests, API calls, or form submissions. Use decoded text for display, database storage, or further processing. Always validate the result matches your expectations.

Tips & Best Practices

  • lightbulb When encoding form data with multiple parameters, encode each value separately but leave the & and = separators unencoded to maintain query string structure
  • lightbulb For URLs with international domains (IDN), use Punycode encoding (xn--) for the domain part, but UTF-8 percent-encoding for path and query parameters
  • lightbulb JavaScript's encodeURIComponent() encodes more characters than encodeURI(). Use encodeURIComponent() for query parameters and encodeURI() for complete URLs
  • lightbulb Avoid the pitfall of manually encoding URLs - always use built-in language functions like encodeURIComponent() in JavaScript, URLEncoder in Java, or urllib.parse.quote() in Python
  • lightbulb For API development, remember that some characters like square brackets [] need encoding in query parameters for array notation: arr[]=1&amp;arr[]=2 becomes arr%5B%5D=1%26arr%5B%5D=2

Frequently Asked Questions

What characters need to be URL encoded? expand_more
Characters that need encoding include: spaces, special characters (&lt;&gt;#%{}|\^~[]`), non-ASCII characters (accented letters, emojis, CJK characters), and reserved characters when used in data (&amp;;=+?$@!). Safe characters that don't need encoding are: A-Z, a-z, 0-9, hyphen (-), underscore (_), dot (.), and tilde (~).
What is the difference between URL encoding and Base64 encoding? expand_more
URL encoding converts special characters to %XX format for safe URL transmission, keeping text human-readable. Base64 converts binary data to ASCII using 64 characters (A-Z, a-z, 0-9, +, /, =), making it unreadable but compact. Use URL encoding for URLs and query parameters; use Base64 for encoding binary data like images in data URLs.
Can URL encoding change the length of my URL? expand_more
Yes, URL encoding typically increases URL length. Each encoded character becomes 3 characters (% + 2 hex digits). A single space becomes %20 (200% longer). International characters like é become %C3%A9 (300% longer). Plan your URL length carefully, especially for systems with URL length limits (IE has 2048 character limit).
Do I need to encode URLs in HTML attributes? expand_more
Yes, always encode URLs in HTML href, src, and other attributes. Browsers may partially encode them, but explicit encoding ensures compatibility. For example: <a href="/search?q=hello%20world">Link</a> instead of <a href="/search?q=hello world">Link</a>. This prevents broken links and security issues.
How do I handle URL encoding in different programming languages? expand_more
Most languages have built-in functions: JavaScript uses encodeURIComponent() for values and encodeURI() for URLs. Python uses urllib.parse.quote() and urllib.parse.urlencode(). Java uses java.net.URLEncoder.encode(). PHP uses urlencode() and rawurlencode(). Always use the standard library functions rather than manual encoding to avoid errors and ensure RFC 3986 compliance.

Related Tools