URL Encoder
Encode and decode URLs using encodeURI and encodeURIComponent. Shows both results side by side with real-time conversion as you type.
Key features
- Encode text to URL-safe format with encodeURIComponent
- Decode URL-encoded strings back to readable text
- Compare encodeURI vs encodeURIComponent results
- Real-time conversion as you type
Guide
URL encoding (also called percent-encoding) converts characters that are not allowed in URLs into a safe format using percent signs and hexadecimal values. Every character that is not an unreserved character (letter, digit, or one of - . _ ~) is replaced with %HH where HH is the character's hexadecimal byte value. A space becomes %20, a forward slash becomes %2F, an ampersand becomes %26, and a plus sign becomes %2B. This tool encodes and decodes URLs and URI components in your browser.
Why URL Encoding Exists
URLs can only contain a limited set of characters defined by RFC 3986. The unreserved characters are A-Z, a-z, 0-9, hyphen (-), period (.), underscore (_), and tilde (~). These characters can appear anywhere in a URL without special meaning.
Reserved characters have special meaning in URL structure: / separates path segments, ? starts the query string, & separates query parameters, = assigns values to parameter names, # marks the fragment identifier, : separates the scheme, @ separates userinfo from host, and [ ] delimit IPv6 addresses. These characters can only be used for their reserved purpose unless percent-encoded.
Any other character, including spaces, non-ASCII characters (accented letters, CJK characters, emoji), and special symbols (< > { } | \ ^ ` and many others) must be percent-encoded to be included in a URL.
Without encoding, ambiguity arises. Consider the URL https://example.com/search?q=rock&roll. Is "roll" a second parameter or part of the search query? The URL parser sees two parameters: q=rock and roll (with no value). If the intended query is literally "rock&roll", the ampersand must be encoded: q=rock%26roll. Encoding removes ambiguity by escaping characters that would otherwise be interpreted as URL syntax.
Another example: the URL https://example.com/path?redirect=https://other.com/page contains a nested URL. The URL parser sees the inner URL's / and ? as part of the outer URL's structure. The redirect parameter must be encoded: redirect=https%3A%2F%2Fother.com%2Fpage.
encodeURIComponent vs. encodeURI in JavaScript
JavaScript provides two encoding functions with different scopes, and choosing the wrong one is one of the most common bugs in web development:
encodeURIComponent() encodes everything except A-Z a-z 0-9 - _ . ! ~ * ' ( ). It is designed for encoding a single component of a URL, such as a query parameter value or a path segment. This is the function you use most often.
encodeURI() encodes less aggressively. It preserves characters that are part of URL structure: : / ? # [ ] @ ! $ & ' ( ) * + , ; =. It is designed for encoding an entire URL string where you want to preserve the URL structure but encode any illegal characters within it (like spaces or non-ASCII characters).
The critical difference shown with an example:
encodeURI("https://example.com/path name?q=hello world") gives https://example.com/path%20name?q=hello%20world (structure preserved, spaces encoded).
encodeURIComponent("https://example.com/path name?q=hello world") gives https%3A%2F%2Fexample.com%2Fpath%20name%3Fq%3Dhello%20world (everything encoded including colons, slashes, question mark, and equals sign).
The rule: use encodeURIComponent() for parameter values, path segments, and any individual piece of data that goes into a URL. Use encodeURI() only when you have a complete URL string that might contain spaces or non-ASCII characters but whose structure (://?&#=) should be preserved.
In practice, you almost always want encodeURIComponent(). The correct pattern for building URLs with dynamic values: const url = "https://api.example.com/search?q=" + encodeURIComponent(userInput) + "&lang=" + encodeURIComponent(language).
Even better, use the URL and URLSearchParams APIs: const url = new URL("https://api.example.com/search"); url.searchParams.set("q", userInput); url.searchParams.set("lang", language); The URL API handles encoding automatically and correctly.
How Percent-Encoding Works Internally
Each byte of the character's UTF-8 representation is written as %HH where HH is the uppercase hexadecimal value of the byte.
ASCII characters use a single byte: space (0x20) becomes %20, ampersand (0x26) becomes %26, plus (0x2B) becomes %2B, hash (0x23) becomes %23, and percent itself (0x25) becomes %25.
Non-ASCII characters use multiple bytes in UTF-8. The Euro sign (U+20AC) is encoded as 3 bytes in UTF-8 (0xE2, 0x82, 0xAC), which becomes %E2%82%AC. The Japanese character for mountain (U+5C71) is encoded as 3 bytes (0xE5, 0xB1, 0xB1), becoming %E5%B1%B1. Emoji characters use 4 bytes, so a single emoji produces 4 percent-triplets (12 characters).
This is why encoded URLs with non-Latin characters get very long. A search query of 10 Japanese characters becomes 90 characters when URL-encoded (10 characters x 3 bytes x 3 characters per encoded byte). Modern browsers display the decoded (human-readable) URL in the address bar for usability, but the actual HTTP request uses the encoded form.
Practical Use Cases
Building API URLs with user input is the most common use case. Any time you construct a URL that includes user-provided data, encode the user input. If a search query contains special characters, encoding prevents them from breaking the URL structure or being misinterpreted.
For example, searching for the phrase "price < $50 & free shipping" requires encoding: encodeURIComponent("price < $50 & free shipping") produces price%20%3C%20%2450%20%26%20free%20shipping. Without encoding, the & would start a new parameter, the < might be stripped by a WAF, and the $ could cause server-side interpretation issues.
Redirect URLs (return URLs) are parameters in OAuth flows, login pages, and single sign-on systems. The redirect_uri parameter contains a complete URL as its value: redirect_uri=https://myapp.com/callback?token=abc. The inner URL's ? and = would collide with the outer URL's query string syntax. Encoding gives redirect_uri=https%3A%2F%2Fmyapp.com%2Fcallback%3Ftoken%3Dabc. OAuth failures are frequently caused by incorrect redirect URI encoding.
Form data: HTML forms with method="GET" automatically URL-encode field values in the query string. Forms with method="POST" and the default enctype (application/x-www-form-urlencoded) also URL-encode the body. When manually constructing form data for fetch() or XMLHttpRequest, you need to handle encoding yourself (or use URLSearchParams which does it for you).
Path segments with special characters: If a file path or resource name contains a slash, question mark, space, or other reserved character, it must be encoded in the URL. A file named "report Q1/Q2 2026.pdf" in a URL path becomes report%20Q1%2FQ2%202026.pdf. The encoded slash %2F is interpreted as a literal character, not a path separator.
Internationalized Resource Identifiers (IRIs): domain names with non-ASCII characters use Punycode encoding (a different system from percent-encoding). The path and query components use percent-encoding. A URL like https://example.com/cafe/menu works in browsers because they encode the non-ASCII e (e with acute accent) automatically, but the canonical encoded form is https://example.com/caf%C3%A9/menu.
Hash/fragment values: Content after # in a URL is the fragment identifier, used for in-page navigation and client-side routing. If your data contains #, it must be encoded as %23 to prevent the URL parser from interpreting it as the start of a fragment. For example, the color code #FF0000 as a parameter value must be encoded: color=%23FF0000.
The Plus Sign Problem
In the application/x-www-form-urlencoded format (used by HTML forms), spaces are encoded as + instead of %20. This is a historical convention from early web forms (HTML 2.0). In the URL standard (RFC 3986), spaces are always %20, and + is a literal plus sign.
This causes real confusion. If a form value contains a literal plus sign (like "C++ programming"), the form encoding produces C%2B%2B+programming. The %2B encodes the literal plus signs, and the + represents the space. When decoded correctly, you get "C++ programming".
But if you use + for spaces in a non-form URL context (like an API call), a literal plus sign in the data gets decoded as a space instead. This is a common bug: the API client encodes data using form encoding rules, but the server decodes using RFC 3986 rules (or vice versa).
Best practice: always use %20 for spaces in URLs. Use + only when working with the application/x-www-form-urlencoded format specifically (like HTML form submissions). This tool always encodes spaces as %20, and its decoder leaves + as a literal plus sign. Replace each + with %20 before you decode form-encoded data here.
URLSearchParams in JavaScript uses + for spaces (form encoding convention). If you need strict RFC 3986 encoding, use encodeURIComponent() which uses %20.
Decoding
Decoding reverses the encoding: every %HH sequence is converted back to the byte represented by the hexadecimal value, and the bytes are interpreted as UTF-8 text. The decoded URL is what the server receives after the URL parser processes the request.
Decoding is useful when you see a URL in a log file, a network trace, a database, or a server error message and want to understand what it says. A log entry like /search?q=%E6%9D%B1%E4%BA%AC%E5%A1%94 is unreadable, but decoding reveals it is a search for a specific Japanese text.
Double decoding is sometimes needed when data has been encoded twice. This happens when a URL-encoded string passes through another encoding step (for example, a form submission that encodes an already-encoded redirect URL). The result contains %25 (the encoded percent sign): %2520 decodes to %20 (first decode), which decodes to a space (second decode). If you see %25 in a URL, it means the percent sign itself was encoded, which is a strong indicator of double encoding.
Triple encoding exists too, though it is rare and always a bug. If you see %252520, that is triple-encoded space.
URL Encoding in Different Languages
JavaScript: encodeURIComponent(value) and decodeURIComponent(value) for components. encodeURI(url) and decodeURI(url) for full URLs. new URLSearchParams({key: value}).toString() for form encoding.
Python: from urllib.parse import quote, unquote, urlencode. quote("hello world") returns hello%20world. quote_plus("hello world") returns hello+world. urlencode({"q": "hello world"}) returns q=hello+world.
Java: URLEncoder.encode(value, StandardCharsets.UTF_8) uses + for spaces (form encoding). For RFC 3986 percent-encoding, use the URI class or manually replace + with %20. This API mismatch has confused Java developers for over two decades.
PHP: urlencode() uses + for spaces (form encoding). rawurlencode() uses %20 (RFC 3986). Use rawurlencode() for path components and URL parameters. Use urlencode() for form data.
Go: url.QueryEscape() for query parameter encoding (uses + for spaces). url.PathEscape() for path segment encoding (uses %20 for spaces).
C#: Uri.EscapeDataString() for component encoding (RFC 3986). Uri.EscapeUriString() for full URI encoding. HttpUtility.UrlEncode() for form encoding.
Ruby: CGI.escape() for form encoding (+ for spaces). URI.encode_www_form_component() for component encoding.
Common Mistakes
Mistake 1: Not encoding user input in URLs. This is both a correctness bug (special characters break the URL structure) and a security vulnerability (unencoded user input can enable open redirect attacks, SSRF, header injection, and other vulnerabilities). Always encode dynamic values.
Mistake 2: Using encodeURI() when you need encodeURIComponent(). If you encode a parameter value with encodeURI(), characters like & and = will not be encoded, breaking the query string structure. encodeURI("rock&roll") returns rock&roll (unchanged because & is a reserved character preserved by encodeURI). encodeURIComponent("rock&roll") returns rock%26roll (correct for a parameter value).
Mistake 3: Double encoding. If your HTTP client library already encodes parameters (like axios, fetch with URLSearchParams, or requests in Python), encoding manually before passing the value results in double encoding. The parameter value "hello world" becomes hello%2520world instead of hello%20world. Check your library's documentation to know whether it handles encoding.
Mistake 4: Decoding path segments incorrectly. A URL like /path%2Fto/file has an encoded slash in the first path segment. The path has two segments: "path/to" and "file", not three segments "path", "to", and "file". The %2F is a literal slash character in the segment name. Web servers and frameworks handle this differently, which can cause routing bugs.
Mistake 5: Forgetting to encode the hash character. If your data contains #, it must be encoded as %23. An unencoded # causes everything after it to be interpreted as the fragment, which is not sent to the server. The URL /search?q=C#%20language sends q=C to the server, losing everything after the #.
Mistake 6: Encoding entire URLs when you should encode only parameter values. Running encodeURIComponent on https://example.com/path?key=value turns it into an unnavigable string where even the :// is encoded. Only encode the dynamic parts.
Mistake 7: Inconsistent encoding between client and server. If the client encodes with encodeURIComponent (which uses %20 for spaces) but the server expects form encoding (which uses +), spaces will be decoded incorrectly. Ensure both sides agree on the encoding convention.
Advanced Tips
When building REST APIs, decide on a consistent encoding strategy for path parameters. If a resource name can contain slashes or special characters, document whether clients should encode them and how the server decodes them. For example, should a GitHub repository named "my/project" be accessed at /repos/my%2Fproject or /repos/my/project? Inconsistent encoding between documentation and implementation is a common source of 404 errors.
For debugging, use your browser's developer tools. The Network tab shows both the raw (encoded) URL and the decoded version. Compare them to verify encoding is correct. You can also use the browser console: decodeURIComponent("%E4%B8%AD%E6%96%87") instantly shows you the decoded text.
When storing URLs in databases, store them in their decoded (human-readable) form and encode when constructing HTTP requests. This makes the stored URLs readable and avoids double-encoding issues. If you must store encoded URLs, document the encoding convention and ensure all code paths use the same convention.
For URLs in HTML attributes, both URL encoding and HTML entity encoding apply. An ampersand in a URL inside an href attribute must be & in the HTML source: <a href="page?a=1&b=2">. Most templating engines (React JSX, Jinja2, Handlebars) handle HTML entity encoding automatically when you use data binding. But if you build HTML strings manually, you need both layers of encoding.
URLSearchParams in JavaScript is the safest way to build query strings. It handles encoding automatically and correctly: new URLSearchParams({q: "rock & roll", page: "1"}).toString() produces q=rock+%26+roll&page=1. This avoids manual encoding mistakes. The URL class also handles encoding: new URL("https://example.com/search"); url.searchParams.set("q", "rock & roll") produces the correct encoded URL.
For testing URL encoding, compare the behavior of your code with this tool. Paste the same input and verify that your application produces the same encoded output. Mismatches indicate a bug in your encoding logic, the wrong encoding function, or double encoding.
URL Encoding and Security
URL encoding is directly related to web security. Several attack vectors exploit improper encoding or decoding.
Open redirect attacks use unvalidated redirect URLs. If your application redirects to a URL from a parameter (/redirect?url=https://evil.com), an attacker can craft a link that appears to be from your domain but redirects to a phishing page. Always validate redirect URLs against an allowlist, not just by checking the domain prefix (https://evil.com.example.com looks like example.com but is not).
Path traversal attacks use encoded characters to navigate the file system. The sequence %2e%2e%2f decodes to ../ which traverses up a directory. A URL like /files/%2e%2e%2f%2e%2e%2fetc/passwd attempts to access /etc/passwd. Web servers should decode URLs before applying security checks, not after.
Server-Side Request Forgery (SSRF) can use URL encoding to bypass allowlists. An attacker might encode internal IP addresses or hostnames in ways that bypass string-matching security checks but decode to the intended target. Defense requires decoding URLs to their canonical form before applying security logic.
Double encoding attacks exploit servers that decode URLs twice. The sequence %252e%252e%252f decodes to %2e%2e%2f on first decode, then to ../ on second decode. If the security check runs after the first decode but before the second, the attack bypasses protection. Servers should decode URLs exactly once.
CRLF injection uses encoded newline characters (%0d%0a) to inject HTTP headers. A URL like /page?header=value%0d%0aSet-Cookie:%20evil=1 attempts to set a cookie through the URL. Modern web frameworks protect against this, but custom URL handling code might be vulnerable.
URL Length Limits
RFC 3986 does not specify a maximum URL length, but practical limits exist. Most browsers support URLs up to about 65,536 characters. Internet Explorer (legacy) had a 2,083-character limit. Apache HTTPD defaults to 8,190 bytes for the request line. Nginx defaults to 4,096 bytes for request headers (which includes the URL in GET requests). Cloudflare limits URLs to 32,768 bytes.
URL-encoded content can be significantly longer than the original text. A 100-character Japanese text becomes about 900 characters when URL-encoded. If you pass long internationalized text as URL parameters, you may hit length limits. For large payloads, use POST requests with a JSON body instead of GET parameters.
When URL length becomes a concern, consider these strategies: use POST instead of GET for large data, shorten URLs using a redirect service, compress the data before URL-encoding it (though this adds complexity), or move parameters to the request body or headers.
Frequently asked questions
What is URL encoding?
URL encoding replaces unsafe characters with percent-encoded equivalents (e.g., space → %20) so they can be safely used in URLs.
What is the difference between encodeURI and encodeURIComponent?
encodeURI keeps URL structure characters such as : / ? # & = intact. encodeURIComponent encodes everything except letters, digits and - _ . ! ~ * ' ( ).
