Learn how to encode and decode URLs safely, with practical workflows for query parameters, path segments, Unicode text, API requests, and double-encoding problems. This guide also gives you a repeatable checklist for testing links and knowing when URL-handling logic needs review.
Overview
URL encoding converts characters that have a special meaning in a web address, or that cannot be transmitted reliably as written, into a percent-encoded form. For example, a space may become %20, while an ampersand may become %26. Decoding reverses that transformation.
The difficult part is that a URL is made of different components, and each component has different rules. A slash in a path can separate two path segments, while a slash inside a query value may simply be data. Encoding the complete URL as one string can therefore change its meaning.
A reliable workflow starts by identifying the value you are handling, choosing the correct component-specific function, and checking the result in a browser, API client, or automated test. Online developer utilities can be useful for quick inspection, but do not paste credentials, session tokens, private customer data, or sensitive production URLs into an unfamiliar URL encoder or decoder.
What to track
1. Separate the URL from its individual values
Before encoding anything, divide the address into its main parts:
- Scheme: such as
https. - Host: the domain name and, where relevant, port.
- Path: the route after the host, such as
/products/red shoes. - Query string: key-value data after
?, such ascolor=red&sort=price. - Fragment: the portion after
#, often used for a page location or client-side state.
Encode a value before inserting it into its destination. If a search term is red shoes & boots, encode the term, not the entire URL. The query structure should remain readable as a query, while the value becomes safe data.
2. Distinguish path encoding from query encoding
Path segments and query parameters are not interchangeable. A path such as /files/annual report.pdf may need its individual segment encoded, while the slash separators should remain separators. A query value such as annual report.pdf?draft=true should encode characters that might otherwise be interpreted as query syntax.
In JavaScript, this commonly means using encodeURIComponent() for an individual query value or path segment, rather than applying it to a complete URL. For a complete URL, use a URL parser and update its components. Similar component-aware methods are available in most backend languages and web frameworks.
3. Recognize common percent-encoded characters
| Character | Encoded form | Why it matters |
|---|---|---|
| Space | %20 | Whitespace is not normally written literally in a URL. |
& | %26 | Separates query parameters, so encode it when it is data. |
= | %3D | Separates a query key from its value. |
? | %3F | Introduces a query string in a URL. |
# | %23 | Introduces a fragment. |
/ | %2F | Separates path segments unless it is encoded as data. |
+ | %2B | Prevents confusion with form-style space handling. |
% | %25 | Represents a literal percent sign. |
Percent encoding represents bytes using a percent sign followed by two hexadecimal characters. Non-ASCII text is commonly converted through UTF-8 before those bytes are represented in a URL. Always test Unicode examples such as accented names, emoji, and non-Latin scripts if your application accepts international input.
4. Check repeated and double encoding
Double encoding occurs when an already encoded value is encoded again. A literal percent sign in %20 can become %2520. If a server decodes only once, the application may receive %20 instead of a space. If different layers decode at different times, routing and security behavior can become difficult to predict.
Track the value at each boundary: browser form, client-side code, redirect, proxy, server framework, application handler, and downstream API. Document where decoding is expected to happen, and avoid “fixing” malformed values by repeatedly decoding until they look correct.
5. Test repeated query parameters and empty values
Do not assume every query parameter appears only once. Applications may receive forms such as ?tag=api&tag=web, an empty value such as ?filter=, or a key without an equals sign. Confirm how your framework represents these cases before changing parsing logic. The same discipline applies to plus signs, semicolons, trailing slashes, and an encoded slash inside a path value.
Cadence and checkpoints
URL encoding is easy to overlook because a basic link may work for months. Add URL checks to normal development and release routines rather than waiting for a visible failure.
During implementation
- Write down whether each input is a complete URL, path segment, query key, query value, or fragment.
- Use a URL parser or framework builder instead of concatenating untrusted strings.
- Test spaces, reserved characters, Unicode, empty values, repeated parameters, and literal percent signs.
- Compare the generated URL with the value received by the server or API.
Before release
Run representative links through browser navigation, redirects, server logs, and API tests. Check that analytics parameters, pagination, filters, search terms, and callback URLs survive each hop. For broader request validation, pair this work with the API testing workflow.
Monthly or quarterly
Review URL construction in shared helpers, routing libraries, reverse proxies, and integration code. A short recurring audit can find new double-encoding paths introduced by a framework upgrade or a refactor. Record a few known-good examples in automated tests so future changes are easier to compare.
How to interpret changes
When a URL changes after a code update, first identify which layer changed it. A browser may display a normalized address, a router may decode a segment, and an API client may serialize query parameters differently. Compare the raw input, serialized URL, received request, and parsed application value rather than relying only on what appears in the address bar.
Common symptoms point to different causes:
- A search term is cut off: an unencoded
&or#may be interpreted as URL syntax. - A space becomes a plus sign: form-style query serialization may be involved. Confirm whether the receiving parser treats plus as a space or as a literal plus.
- A route returns not found: a slash may have been encoded as data, or a path segment may have been decoded too early.
- The API receives
%20instead of a space: the value may have been encoded twice or decoded too few times. - Unicode text is corrupted: inspect character encoding at the client, transport, server, and database boundaries.
- A redirect loses parameters: check whether the redirect builder treats the existing query string as a raw string or as structured key-value data.
Use browser network tools to inspect the actual request and response headers. The Browser DevTools debugging checklist can help when the visible link differs from the request sent by the page. For API payloads or related structured data, a JSON formatter and validator can make comparisons easier without replacing request-level testing.
Security deserves separate attention. Treat decoded URL data as untrusted input, validate allowed schemes and hosts for redirect or callback features, and do not assume that encoding itself makes a value safe. Encoding preserves data representation; it does not authorize a destination or remove the need for input validation.
When to revisit
Revisit this guide and your URL tests on a monthly or quarterly cadence, and immediately after changes to routing, redirects, API clients, form serialization, proxy configuration, or URL-related framework utilities. Also review the implementation when users report broken search links, missing filters, failed callbacks, unexpected 404 responses, or parameters that differ between browser and server logs.
For each review, choose a small test matrix:
- A plain word and a value containing spaces.
- A value containing
&,=,?,#,+, and%. - One Unicode value and one emoji if the product accepts user-generated text.
- A path segment containing a slash as data.
- Repeated parameters, empty parameters, and a pre-encoded value.
- A complete URL containing an existing query string and fragment.
Then record the expected serialized URL and the expected parsed values. If results differ, isolate the first boundary where they changed instead of adding another encode or decode call. Keep the working examples in unit or integration tests, and use an online URL encoder or decoder only for non-sensitive sample data. This routine turns URL handling from a one-time debugging task into a maintainable part of your developer toolkit.