Everything works in staging until a user with the email address alex+test@example.com tries to filter their account history, or a customer searches your store for AT&T chargers. Suddenly, your backend receives alex test@example.com (with a space instead of a plus sign), and the search query gets chopped in half right at the ampersand.
URL encoding bugs are easy to miss because a slightly malformed URL rarely crashes the browser outright—it just silently alters the data reaching your server. Understanding how percent-encoding handles spaces, plus signs, reserved delimiters, and UTF-8 characters will help you track down odd API bugs much faster.
Reserved vs. Unreserved Characters: Why Percent-Encoding Exists
Under IETF RFC 3986, a URL can only contain a strict subset of standard ASCII characters. Those characters fall into two buckets:
- Unreserved characters: Uppercase and lowercase letters (
A–Z,a–z), digits (0–9), and four safe punctuation marks: hyphen (-), underscore (_), period (.), and tilde (~). These never need to be encoded. - Reserved characters: Structural symbols like
/,?,&,=,#,:, and+that have special meaning inside a URL.
Because the question mark starts a query string, the ampersand separates key-value pairs, and the hash symbol starts a client-side fragment, you cannot drop those characters directly into a parameter value without confusing the parser. Percent-encoding solves this by replacing the character with a % sign followed by its two-digit hexadecimal byte value:
| Character | Structural Job in a Raw URL | Percent-Encoded Value | What Happens If Left Unencoded in a Query Value |
|---|---|---|---|
Space |
Not allowed in valid URLs | %20 (or + in form queries) |
Can break link auto-detection in emails, logs, and strict HTTP parsers. |
& |
Separates query parameters | %26 |
Splits your value in half and starts a brand-new query parameter. |
+ |
Shorthand for a space in query strings | %2B |
Decoded by backend frameworks as a blank space (breaking emails and Base64 tokens). |
# |
Starts a browser fragment identifier | %23 |
Everything from # onward is stripped by the browser and never sent to the server. |
= |
Separates a parameter key from its value | %3D |
Can confuse naive query parsers when parsing nested tokens or Base64 padding. |
/ |
Separates URL path segments | %2F |
Can break routing rules when passed inside a path parameter. |
Notice the row for #: if a user searches your site for C# programming and your front end builds the URL /search?q=C# programming without encoding the hash as %23, the browser treats # programming as a local page anchor. Your server only receives ?q=C!
The Space vs. Plus Sign (%20 vs. +) Bug
Why do some URLs encode spaces as %20 while others replace spaces with +? The split comes down to two different specifications colliding inside the same URL:
- Path segments follow RFC 3986: Inside the path portion of a URL (before the
?), a space is always encoded as%20. A literal+inside a URL path is treated as an actual plus sign. - Query strings often follow HTML form encoding (
application/x-www-form-urlencoded): When an HTML form or JavaScript'sURLSearchParamsbuilds a query string (after the?), spaces are converted into+, and actual plus signs are encoded as%2B.
This creates a classic bug when developers build URLs by concatenating strings manually:
// Buggy manual string concatenation
const email = "alex+dev@example.com";
const url = `https://api.example.com/users?email=${email}`;
When your backend web framework (Express, Laravel, Django, or Spring) parses ?email=alex+dev@example.com, it follows the form-urlencoded rule and turns the unencoded + into a space—looking up alex dev@example.com in your database and returning zero results. Always encode the parameter value so + becomes %2B.
encodeURI() vs. encodeURIComponent() in JavaScript
JavaScript provides two built-in encoding functions that look nearly identical in autocomplete, but using the wrong one on query parameters will break your requests.
As detailed in MDN's encodeURIComponent() documentation:
encodeURI(fullUrl)is designed for a complete URL. It encodes spaces and non-ASCII characters, but it deliberately leaves: / ? # & = + $ , ;untouched so it does not destroy the structure of the URL itself.encodeURIComponent(value)is designed for a single parameter key or value. It escapes every reserved delimiter—including&,=,+,/, and#—so the value can be safely dropped into a query string.
const userInput = "alex+dev@example.com&role=admin";
// WRONG: encodeURI leaves + and & and = unencoded!
console.log(encodeURI(userInput));
// Output: "alex+dev@example.com&role=admin" (the & now starts a new parameter!)
// RIGHT: encodeURIComponent escapes +, @, &, and =
console.log(encodeURIComponent(userInput));
// Output: "alex%2Bdev%40example.com%26role%3Dadmin"
Better yet, in modern JavaScript, you can avoid manual string concatenation altogether by using the native URL and URLSearchParams APIs, which automatically encode every parameter value for you.
3 Classic Encoding Traps in Production Apps
1. Double-Encoding (The Dreaded %2520)
Ever noticed a URL containing %2520 instead of %20? Because the ASCII hex code for the percent sign (%) itself is 25, running a string that is already percent-encoded through an encoder a second time turns %20 into %2520:
const once = encodeURIComponent("hello world"); // "hello%20world"
const twice = encodeURIComponent(once); // "hello%2520world"
This usually happens when you manually call encodeURIComponent() on a value and then pass that already-encoded string into an HTTP client library (like Axios or URLSearchParams) that encodes parameters automatically.
2. Unencoded Redirect URLs in Login Flows
Suppose an unauthenticated user visits https://example.com/dashboard?tab=billing&plan=pro, and your app redirects them to the login screen with a return URL:
// Broken: &plan=pro gets detached from the redirect parameter!
https://example.com/login?redirect=https://example.com/dashboard?tab=billing&plan=pro
Because the nested URL was not encoded, the login page thinks redirect equals https://example.com/dashboard?tab=billing and treats plan=pro as a separate parameter for the login page itself. Encoding the redirect target with encodeURIComponent() keeps the entire target URL intact.
3. Non-ASCII (UTF-8) URLs in Sitemaps, Canonicals, and robots.txt
When a URL path contains Arabic, Chinese, Cyrillic, or accented Latin characters (for example, https://example.com/café), browsers display the readable letters in the address bar, but over the network they convert each UTF-8 byte into percent-encoded octets (https://example.com/caf%C3%A9).
When managing technical SEO files, keep these rules in mind:
- In
robots.txt, RFC 9309 says non-ASCII characters in rules are matched in percent-encoded form, so Google treats raw UTF-8 paths and percent-encoded paths as the same (other crawlers may behave differently), as we explained in our guide on configuring a robots.txt file. - In canonical tags and XML sitemaps, always use properly escaped URLs so your canonical tags and XML sitemaps match the exact URL format your server responds to.
Inspecting, Encoding, and Decoding URLs in Your Browser
When you are debugging a broken webhook URL, a deeply nested OAuth redirect link, or a garbled query string from server logs, decoding hex bytes in your head is slow and error-prone.
You can clean up and inspect links directly in your browser using three client-side utilities:
- Encode raw parameter values: Paste strings containing spaces, plus signs, ampersands, or UTF-8 characters into the URL Encoder to convert them into safe percent-encoded strings.
- Read messy log URLs: Drop percent-encoded strings (`%2520`, `%3A%2F%2F`, or multi-byte UTF-8 sequences) into the URL Decoder to reveal the readable plaintext immediately.
- Break apart complex links: If a tracking link or OAuth callback has a dozen chained query parameters, paste it into the URL Parser to view the protocol, hostname, path, and individual key-value pairs cleanly separated.
Frequently Asked Questions (FAQ)
What is the difference between urlencode() and rawurlencode() in PHP?
In PHP, urlencode() follows the older HTML form specification and encodes spaces as + signs. By contrast, rawurlencode() follows RFC 3986 and encodes spaces as %20. Use rawurlencode() when encoding URL path segments or working with modern REST APIs that expect strict RFC 3986 percent-encoding.
Are percent-encoded hex characters case-sensitive (`%2f` vs. `%2F`)?
Any compliant URL decoder will read both %2f and %2F as a forward slash. However, RFC 3986 Section 2.1 recommends always generating uppercase hexadecimal digits (%2F, %3A, %C3%A9) for consistency—especially when comparing URLs for caching or canonicalization, where simple string comparisons treat lowercase and uppercase strings as different.
Does URL encoding protect my website against SQL Injection or XSS?
No. URL encoding is purely a transport formatting rule so characters survive the trip inside a URL without breaking its structure. Web servers automatically decode percent-encoded parameters before handing them to your application code. You still need parameterized database queries and proper HTML output escaping on the server.