URL Encoding Guide
URL Encoding Guide covering percent-encoding, UTF-8, query strings, paths, spaces, plus signs, double encoding, security, and practical code examples.
Quick Tool
Encode or decode a URL value now
Start with the URL Encoder or URL Decoder, then use the guide below to understand percent-encoding, UTF-8, query strings, path segments, plus signs, and double-encoding mistakes.
What is URL encoding?
URL encoding, more precisely called percent-encoding, represents bytes with a percent sign followed by two hexadecimal digits. A space may become %20, a slash may become %2F, and the UTF-8 bytes for non-ASCII text are encoded one byte at a time.
This URL Encoding Guide focuses on component-level encoding. A URL contains different components—scheme, host, path, query, and fragment—and each component has different syntax rules. A character that is safe in one component may be structural or unsafe in another.
For normative behavior, consult the WHATWG URL Standard and MDN percent-encoding reference.
How to use the URL Encoder and Decoder
- Paste the exact URL component you want to encode or decode. Work with a query value, parameter name, or path segment instead of encoding a complete URL blindly.
- Choose the direction you need: encode plain text into percent-encoded form or decode an encoded value back to readable text.
- Run the tool and compare the result with the original value. Pay special attention to spaces, plus signs, ampersands, slashes, percent signs, and non-ASCII characters.
- Use the result in the real query string, path, redirect, or API request, then parse the finished URL again if you need to confirm that each component still has the intended meaning.
Examples
Encode a query value
blue & green -> blue%20%26%20greenThe ampersand must be encoded when it belongs inside a value because a literal ampersand separates query parameters. A normal space becomes %20 in generic percent-encoding.
Encode a dynamic path segment
const path = `/users/${encodeURIComponent(userId)}`;Encode the dynamic segment rather than the whole URL. This keeps structural characters such as the scheme, host, slashes, question mark, and fragment delimiters intact.
Preserve a literal plus sign
+ -> %2BIn form-style query decoding, a plus sign can represent a space. Encode a literal plus as %2B when the receiving parser would otherwise turn it into whitespace.
Edge-case lab
URL Encoding Test Matrix
Use these cases to verify the exact component you are encoding. The same character can be data in one context and URL structure in another, which is why component-level testing matters.
| Case | Input | Correct handling | Why it matters |
|---|---|---|---|
| Space in a query value | blue sky | blue%20sky | Generic percent-encoding represents a space as %20. Form encoding may use +, so match the convention expected by the receiver. |
| Literal plus sign | C++ | C%2B%2B | A literal + can be interpreted as a space by form-style query parsers. %2B preserves the plus character itself. |
| Ampersand inside data | R&D | R%26D | An unescaped ampersand separates query parameters. Encode it when the ampersand belongs to the value. |
| Slash inside one path segment | reports/2026 | reports%2F2026 | Encode the slash only when it is data inside a segment. Leave structural path separators unencoded. |
| UTF-8 text | café ✓ | caf%C3%A9%20%E2%9C%93 | Non-ASCII characters are first represented as UTF-8 bytes and then percent-encoded byte by byte. |
| Emoji | rocket 🚀 | rocket%20%F0%9F%9A%80 | Emoji use multiple UTF-8 bytes, so one visible character becomes several percent escapes. |
| Already encoded value | hello%20world | hello%2520world if encoded again | Encoding the percent sign a second time produces %25 and creates the classic double-encoding bug. |
| Incomplete percent escape | price%2 | Treat as malformed input | A percent escape needs exactly two hexadecimal digits. Do not silently assume what the missing digit should be. |
| Nested return URL | next=https://example.com/a?x=1&y=2 | next=https%3A%2F%2Fexample.com%2Fa%3Fx%3D1%26y%3D2 | The inner URL is data in the outer query parameter, so its structural characters must be encoded at the outer level. |
| Form-style space | hello world | hello+world (form) / hello%20world (generic) | application/x-www-form-urlencoded commonly uses + for spaces; generic URI component encoding uses %20. |
Choose the encoding rule by context
| Context | Encode | Usually preserve |
|---|---|---|
| Query parameter value | Reserved characters that belong to the value | The outer = and & separators |
| Path segment | Characters that are data inside that segment | Structural / separators between segments |
| Nested URL parameter | The entire inner URL as one outer value | The outer URL structure |
| HTML form query | Use form-url-encoded rules | Only separators created by the form serializer |
Check: A successful decode is not proof that the original URL was encoded at the correct layer. Always test the final URL in the parser or application that will consume it.
Common use cases
API query parameters
Encode parameter names and values before joining them with = and &. This prevents reserved characters inside data from changing the structure of the request.
Search and filter URLs
User-entered search terms often contain spaces, ampersands, punctuation, or Unicode. Component-level encoding keeps those values intact when they are added to a search URL.
Dynamic path segments
IDs, filenames, usernames, and other path values may contain characters with structural meaning. Encode the individual segment and test how the real router handles encoded slashes or delimiters.
Redirect and return URLs
A return URL placed inside another URL is data at the outer level. Encode it once for the correct component, then validate the decoded destination before redirecting.
Form submissions
application/x-www-form-urlencoded uses conventions that differ from generic percent-encoding, especially for spaces and plus signs. Use the convention expected by the receiving endpoint.
Advanced tips
Encode components, not complete URLs
The scheme, host, slash, question mark, ampersand, equals sign, and fragment marker define URL structure. Encoding everything at once can destroy that structure.
Keep UTF-8 handling explicit
Percent-encoding represents bytes. Modern URL tooling normally converts text to UTF-8 first, so non-ASCII characters may become several percent-encoded byte values.
Do not confuse %20 and +
A generic URL space is %20. A plus can mean space in form-style query parsing, but a plus in a path normally remains a literal plus unless it is encoded.
Avoid repeated decode cycles
Double encoding can create values such as %252F, where decoding once produces %2F and decoding again produces /. Track the expected encoding depth instead of repeatedly decoding until the text looks readable.
Validate after decoding
Percent-encoding is not a security boundary. Validate a decoded redirect target, path, HTML value, SQL value, or file-related value before using it in a sensitive operation.
Reserved and unreserved URL characters
Unreserved characters are generally safe to use without encoding: ASCII letters, digits, hyphen, period, underscore, and tilde. Reserved characters may act as separators or delimiters and therefore must be handled according to context.
| Group | Characters | Meaning |
|---|---|---|
| Unreserved | A-Z a-z 0-9 - . _ ~ |
Normally remain literal |
| General delimiters | : / ? # [ ] @ |
Define URL structure |
| Sub-delimiters | ! $ & ' ( ) * + , ; = |
Used by components and protocols |
How percent-encoding works
Percent-encoding operates on bytes. The ASCII character / has byte value 47, hexadecimal 2F, so its encoded representation is %2F. Hexadecimal digits are case-insensitive, although uppercase output is common.
Original: blue & green
Encoded: blue%20%26%20green
The ampersand is encoded because it separates parameters inside a query string. Leaving it literal inside a value can change the meaning of the request.
URL Encoding Guide to UTF-8 characters
Modern URLs normally encode text as UTF-8 before percent-encoding the resulting bytes. The character ✓ becomes the three UTF-8 bytes E2 9C 93, represented as %E2%9C%93.
Do not encode Unicode code points directly. Use a URL API or a language function that converts the input to UTF-8 bytes correctly.
Spaces: %20 versus plus
In generic URL percent-encoding, a space is %20. In application/x-www-form-urlencoded form data and many query-string parsers, a plus sign can represent a space. These conventions are related but not identical.
Generic component: John%20Smith
Form query value: John+Smith
Warning: A literal plus sign may need encoding as
%2B. Otherwise a form-style decoder may turn it into a space.
Encoding query-string names and values
Encode each parameter name and value separately, then join them with = and &. Do not encode the separators themselves.
search=blue%20shoes&sort=price%3Aasc
Use the Query String Parser to inspect repeated keys, blank values, nested names, plus signs, and decoded parameter values.
Encoding URL path segments
Encode each dynamic path segment independently. If a user-controlled identifier contains a slash, encoding it as %2F may still be interpreted differently by proxies, routers, or servers. Test the complete request path in the actual infrastructure.
const path = `/users/${encodeURIComponent(userId)}`;
Do not apply query-string form rules automatically to paths. A plus sign in a path normally remains a plus sign rather than representing a space.
URL encoding examples in common languages
JavaScript
const value = encodeURIComponent("blue & green");
const text = decodeURIComponent(value);
encodeURIComponent() is usually the correct choice for an individual query value or path segment. encodeURI() preserves more URL syntax and is not interchangeable.
PHP
$encoded = rawurlencode("blue & green");
$decoded = rawurldecode($encoded);
rawurlencode() follows percent-encoding with spaces as %20. urlencode() uses form-style behavior with plus signs for spaces.
Python
from urllib.parse import quote, unquote
encoded = quote("blue & green", safe="")
decoded = unquote(encoded)
Double encoding and partial decoding
Double encoding happens when an already encoded value is encoded again. The percent sign in %20 becomes %25, producing %2520. One decoding pass returns %20; a second pass returns a space.
Original: a b
Encoded once: a%20b
Encoded twice: a%2520b
Track where encoding occurs and define ownership. A framework, HTTP client, template helper, or browser API may already encode parameters automatically.
Common URL encoding mistakes
- Encoding the entire URL instead of individual components.
- Using
encodeURI()when a query value requiresencodeURIComponent(). - Treating plus as a space in every URL component.
- Decoding a value repeatedly without knowing its original encoding depth.
- Building query strings by string concatenation instead of a URL API.
- Assuming encoded text is encrypted or private.
- Failing to validate the decoded value before using it in redirects, paths, SQL, HTML, or file operations.
Security considerations
Encoding changes representation, not trust. After decoding, validate the result according to its destination. Open redirects, path traversal, header injection, cross-site scripting, and signature mismatches can involve differently encoded but semantically equivalent values.
Security filters should normalize carefully and avoid inconsistent decoding between gateways, application servers, and frameworks. Never log sensitive query parameters, tokens, reset links, or personal data merely because they are percent-encoded.
URL encoding versus Base64 and HTML entities
URL encoding versus Base64
URL encoding escapes selected bytes so data can appear safely in a URL component. Base64 converts arbitrary bytes into a restricted text alphabet and usually increases size more significantly. Neither method provides encryption.
URL encoding versus HTML entity encoding
HTML entities protect text inside HTML contexts, while URL encoding protects URL components. Use the encoder required by the output context; applying the wrong encoder can still leave security vulnerabilities.
A reliable URL Encoding Guide workflow
- Identify the exact URL component being created.
- Keep the original unencoded value.
- Use a standard URL API or component encoder.
- Inspect the encoded output.
- Decode once and compare with the original.
- Test spaces, plus signs, percent signs, slashes, ampersands, Unicode, and empty values.
- Send the result through the real client, proxy, router, and server.
- Validate the decoded value at its destination.
Use URL Encoder to encode component text, URL Decoder to inspect percent sequences, and URL Parser to separate complete URLs into scheme, host, path, query, and fragment.
URL Encoding Guide summary
This URL Encoding Guide recommends encoding individual components with a standard API, preserving structural separators, understanding %20 versus plus, avoiding double encoding, and validating decoded data. Percent-encoding makes URLs syntactically safe; it does not make untrusted data secure or confidential.
Practice what you learned
Related Trexmi tools
Open a focused workspace and test the patterns from this guide.Clear answers
Frequently asked questions
What is URL encoding?+
URL encoding represents bytes as percent signs followed by hexadecimal values so text can be used safely inside URL components.
Should spaces use %20 or plus?+
Generic URL components use %20. Form-encoded query data may use plus to represent a space.
What is double URL encoding?+
Double encoding occurs when an encoded value is encoded again, turning a percent sign such as % into %25.
Should I encode a complete URL?+
Usually no. Encode individual dynamic components while preserving structural separators such as colon, slash, question mark, equals, and ampersand.
Is URL encoding secure or encrypted?+
No. It changes representation only. Anyone can decode it, so sensitive values still require proper transport and access controls.
What is the difference between encodeURI and encodeURIComponent?+
encodeURI preserves many URL structural characters, while encodeURIComponent is intended for individual values or segments and escapes more characters.
How are Unicode characters URL encoded?+
They are converted to UTF-8 bytes and each required byte is represented with a percent sign and two hexadecimal digits.
Why can a plus sign decode as a space?+
Form-style application/x-www-form-urlencoded parsing commonly treats plus as a space in query values. A literal plus should be encoded as %2B.