Home › Guides › Developer › URL structure and percent-encoding

URL structure and percent-encoding explained

What the parts of a URL are, which characters must be percent-encoded, and when to use encodeURIComponent or encodeURI.

By the tools4free.dev team · Last reviewed 4 December 2026

A URL is built from a scheme, an optional authority (user information, host and port), a path, an optional query and an optional fragment. Percent-encoding writes any other byte as % followed by two hexadecimal digits, so that characters with a special meaning in one part of a URL can still be used as data. In JavaScript, encodeURIComponent encodes a single value and encodeURI encodes a whole URL, and choosing the wrong one is the most common encoding bug.

The parts of a URL

RFC 3986 defines the generic syntax. Take this address:

https://user:pw@example.com:8080/a%20b/caf%C3%A9?q=1&r=a%20b#frag
PartValue in the exampleNotes
SchemehttpsLetters, digits, +, - and .; ends at the first :. Case-insensitive.
User informationuser:pwBefore the @. Passwords in URLs are deprecated and browsers often strip or warn about them.
Hostexample.comA name or an IP address. Case-insensitive.
Port8080Optional. Omitted when it is the default for the scheme (443 for https).
Path/a%20b/caf%C3%A9Segments separated by /. Case-sensitive on most servers.
Queryq=1&r=a%20bAfter the first ?. The key=value&key=value layout is a convention, not part of RFC 3986.
FragmentfragAfter the first #. Handled by the browser and never sent to the server.

The fragment point matters in practice. Because it stays in the browser, server logs and analytics that read the request line never see it, and a redirect that drops it can silently break deep links.

Reserved and unreserved characters

RFC 3986 splits ASCII characters into two groups. Unreserved characters are the letters, the digits and - . _ ~. They never need encoding. Reserved characters are the delimiters: : / ? # [ ] @ ! $ & ' ( ) * + , ; =. A reserved character used for its delimiter job is written as it is. The same character used as data must be percent-encoded, or the URL means something different.

Everything else, including spaces, quotation marks, % itself and all non-ASCII text, must be percent-encoded. Non-ASCII characters are first converted to UTF-8 bytes, and each byte is encoded. The euro sign has the UTF-8 bytes E2 82 AC, so it becomes %E2%82%AC. An emoji needs four bytes, so U+1F600 becomes %F0%9F%98%80.

Worked example 1: encodeURIComponent versus encodeURI

Take the string café & crème/100% ?x=1#y, which contains non-ASCII letters, spaces and most of the delimiters. We ran both functions in Node:

encodeURIComponent(s)
// caf%C3%A9%20%26%20cr%C3%A8me%2F100%25%20%3Fx%3D1%23y

encodeURI(s)
// caf%C3%A9%20&%20cr%C3%A8me/100%25%20?x=1#y

encodeURIComponent encodes every reserved character, so the result is safe as one query value or one path segment. encodeURI leaves & / ? # = and the other delimiters alone, because it assumes you are passing an entire URL whose structure you want to keep. Both still encode the space, the % and the accented letters.

The rule is simple. Encode each piece with encodeURIComponent and then join the pieces. Use encodeURI only on a complete URL that is already correctly structured but may contain spaces or non-ASCII characters. Running encodeURI on a value that contains & leaves the ampersand live, and it splits your parameter in two.

One edge case: neither function encodes ! ' ( ) *. In Node, encodeURIComponent("!'()*~-_.") returns the string unchanged. That is fine for query values in practice, but if you are building a value for a system that treats those characters as delimiters, encode them by hand.

Worked example 2: spaces, plus signs and form encoding

A space can appear in a query as %20 or as +. Only the second form is a special case: the HTML form format (application/x-www-form-urlencoded) uses + for a space and %2B for a real plus sign. URLSearchParams follows that format:

new URLSearchParams({ q: 'a b&c', n: '1+1' }).toString()
// q=a+b%26c&n=1%2B1

decodeURIComponent('a+b%2Bc')
// "a+b+c"

Notice the last line. decodeURIComponent does not turn + into a space, because it follows RFC 3986 rather than the form format. If you decode a query string by hand, replace + with a space first, then decode. In a path, a + is just a plus sign, so a space there must be %20.

Parsing and normalising with the URL class

The WHATWG URL class, available in browsers and Node, parses an address into its parts and normalises it as it goes. These results come from Node:

Inputnew URL(input).href
HTTP://EXAMPLE.com:80/http://example.com/ (scheme and host lower-cased, default port removed)
https://example.com/a/../b/./c?x=1https://example.com/b/c?x=1 (dot segments resolved)
https://example.com/a b/caféhttps://example.com/a%20b/caf%C3%A9
https://bücher.example/host becomes xn--bcher-kva.example (Punycode)

Relative references resolve against a base: new URL('../x', 'https://example.com/a/b/c').href gives https://example.com/a/x. Internationalised host names are not percent-encoded. They use Punycode under the IDNA rules, which is why only the host part changes in the last row above.

Double encoding and decoding too early

Encoding is not idempotent. Encode a string that is already encoded and every % becomes %25, so caf%C3%A9 turns into caf%25C3%25A9. The server then decodes once and sees the literal text caf%C3%A9. The reverse mistake is decoding a whole URL before splitting it. If a value contains an encoded %26 and you decode the full query string first, that ampersand becomes a real delimiter and the value is cut short. Split first, decode each piece second.

Decoding can also fail. A lone percent sign is not valid, and in Node decodeURIComponent('100%') throws URIError: URI malformed. Wrap decoding of untrusted input in a try block.

Why it matters beyond encoding bugs

Every URL on a site is an identifier. Two addresses that differ only in letter case in the path, in a trailing slash, or in the order of query parameters can serve identical content yet count as separate pages for caching, analytics and search. Settle on one form, redirect the others to it, and generate links from a single place in your code so the encoding rules are applied once.

Common mistakes

Frequently asked questions

Is a URL case-sensitive?

The scheme and host are not. The path and query usually are, because most web servers treat them as exact text. /About and /about can be different pages.

What is the difference between a URI and a URL?

A URL is a URI that says where a resource is and how to reach it. RFC 3986 defines the syntax for both, and web developers use the terms almost interchangeably.

Is there a maximum URL length?

RFC 3986 sets no limit. Browsers, servers and proxies each set their own, so very long URLs should be avoided. Put large data in a request body instead.

Should I encode a slash in a path segment?

Only if the slash is data, such as a file name that contains one. Then encode it as %2F. Be aware that some servers refuse or rewrite encoded slashes.

Try it: paste any address into the URL parser to see its parts and query parameters, or convert text with the encoder and decoder. Both run in your browser.

Sources