Every developer uses URLs. Most can't cleanly name the difference between the origin and the authority, or say when percent-encoding is required versus cosmetic — and have broken something because of it.
URLs are infrastructure. You type them, build them, parse them, and pass them around all day. And yet the URL is one of those things everyone uses fluently and few can describe precisely — which is fine until the imprecision causes a bug: a broken link from an unencoded space, a CORS failure you don't understand, a signature that won't validate because you reordered the query string. The formal definition, from the RFC that governs the syntax, is deceptively simple:
"A Uniform Resource Identifier (URI) is a compact sequence of characters that identifies an abstract or physical resource."
— IETF RFC 3986, "Uniform Resource Identifier (URI): Generic Syntax"
Simple to state, full of edges in practice. Here are the parts that actually matter, with one real URL taken apart so every term has something concrete to point at.
The anatomy, properly labelled
A full URL is scheme://userinfo@host:port/path?query#fragment. Most people can name scheme, host, path, and query. The ones worth knowing are the ones people skip. userinfo lets you embed credentials directly in a URL (https://user:pass@host) — a genuinely dangerous pattern that leaks passwords into logs and browser history. RFC 3986 says plainly that the user:password form in userinfo "is deprecated". port is implicit when it's the scheme default (443 for HTTPS) and explicit otherwise, and forgetting it matters more than you'd think, as we'll see.
Here is one deliberately busy URL, run through the WHATWG URL parser that ships in Node and every modern browser (the output is saved with this draft's checks):
https://user:pw@shop.example.com:8443/a%20b/c+d?q=red+shoes&size=42&q=blue#reviews
| Part | Value the parser reports |
|---|---|
| protocol (scheme) | https: |
| username / password | user / pw |
| hostname | shop.example.com |
| port | 8443 |
| host | shop.example.com:8443 |
| origin | https://shop.example.com:8443 |
| pathname | /a%20b/c+d |
| search (query) | ?q=red+shoes&size=42&q=blue |
| hash (fragment) | #reviews |
Three things in that table surprise people. The origin does not include the username or password. The + in the path is still a literal plus. And q appears twice — legal, common in filter UIs, and a source of bugs in code that assumes one value per key (searchParams.get("q") returns only red shoes; getAll("q") returns both).
Origin vs authority
These two look almost identical and fail in different places. Origin is scheme + host + port. Authority is userinfo + host + port. The distinction is load-bearing for the same-origin policy and CORS, which compare origins — which is why http:// and https:// versions of the same host are different origins, and why example.com and example.com:8080 are too. If a cross-origin request is being blocked and the hosts "look the same," check the scheme and the port — one of them is almost always different, and the browser is treating them as separate origins exactly as specified.
Cookies use a third, looser idea. SameSite decisions are made on the site, not the origin: the registrable domain plus, in modern browsers, the scheme. MDN's glossary entry for "site" shows https://example.com:8080 and https://example.com as the same site because the port is ignored, while support.mozilla.org and developer.mozilla.org share the site mozilla.org. So a request can be cross-origin and still same-site. Mixing up the two explains a lot of "my cookie is sent but CORS still fails" confusion.
The lookalike-host trick
Userinfo has a security cost beyond leaked passwords. Everything before the @ is userinfo, so https://bank.example.com@evil.test/ sends the browser to evil.test; the parser reports exactly that hostname. A human skimming left to right sees a familiar bank name. RFC 3986 anticipates this and asks applications that display URLs to render userinfo distinctly. In your own code, the defence is simple: never decide where a URL goes by string-matching its start. Parse it and compare hostname (or better, origin) against an allow-list.
Percent-encoding: required vs cosmetic
Encoding isn't uniform across the URL, which is the source of a classic bug. A space in the path must be %20 — a literal + stays a plus sign. A space in the query string can be either %20 or +, because application/x-www-form-urlencoded defines + as a space there. So the same space encodes differently depending on which part of the URL it's in, and code that assumes one rule everywhere breaks the other place.
The JavaScript helpers make the split visible. encodeURIComponent("a b&c=d/é") gives a%20b%26c%3Dd%2F%C3%A9: it encodes the delimiters &, = and /, so the result is safe inside one component. encodeURI leaves those delimiters alone because it assumes you passed it a whole URL. And URLSearchParams serialises Ana María as Ana+Mar%C3%ADa — form encoding, plus for space. Pick the helper by what you are encoding: a single value, a whole URL, or a form body.
Two rules from RFC 3986 prevent most double-encoding bugs. Percent-encoding happens when a URL is assembled from its parts, and the same string must never be encoded or decoded twice. If you see %2520 in a log, something encoded an already-encoded %20.
The fragment never reaches the server
Everything after # — the fragment — is stripped by the browser before the HTTP request goes out. RFC 3986 describes it as "separated from the rest of the URI prior to a dereference". It never appears in your server logs, never reaches your backend, and can't be read server-side. That's why single-page apps used #/route style URLs for years: changing the fragment re-routes the app without a server round-trip. It also has a privacy upside: some apps put one-time tokens in the fragment precisely so they stay out of server access logs. If you're trying to read a fragment on the server, stop — it was never sent.
Query-string order is preserved, but rarely guaranteed to matter
The query is defined by RFC 3986 as a string, not as a set of key-value pairs; the key=value&key=value convention comes from form encoding layered on top. Parsers keep the order they receive: new URLSearchParams("b=2&a=1&c=3").toString() returns the same order, and only an explicit sort() changes it to a=1&b=2&c=3. The trouble is that systems disagree on whether order is meaningful. Some caches normalise parameter order so ?a=1&b=2 and ?b=2&a=1 share a cache entry; others treat them as different keys. Request-signing schemes often require a specific (frequently sorted) order, so reordering silently invalidates the signature. The safe rule: never depend on a third party preserving your order, and when signing, sort exactly as the signer's documentation says.
Relative URLs resolve in ways that surprise
Relative references are resolved against a base, and the dot-segment rules are precise. Resolving ../g against http://a/b/c/d;p?q gives http://a/b/g — the last segment of the base (d;p) is dropped first, then .. climbs one level. That is straight from the RFC's own examples, and Node produces the same answer. The practical bug is a missing trailing slash: a base of /docs/guide resolves intro to /docs/intro, while /docs/guide/ resolves it to /docs/guide/intro. If relative links work on one page and break on another, compare the trailing slashes first.
Common mistakes, quickly
- Building URLs with string concatenation. One forgotten
?or&, or one unencoded value containing&, corrupts every parameter after it. - Comparing URLs as raw strings.
HTTP://Example.com:443/andhttps://example.com/can identify the same resource; scheme and host are case-insensitive and default ports are dropped during normalisation. - Trusting
startsWith("https://good.example"). It also matcheshttps://good.example.evil.test/. Compare parsed hostnames instead. - Assuming one value per key. Repeated parameters are valid; decide deliberately whether your code takes the first, the last, or all of them.
FAQ
Is a URL the same as a URI? A URL is a URI that also tells you how to locate the resource. RFC 3986 recommends the general term "URI" for specifications; in day-to-day web work the two are used interchangeably.
Should I put an API key in the query string? Avoid it where you can. Query strings end up in server logs, proxy logs and browser history. A request header is the usual alternative.
Why does my server see %2F decoded in some frameworks and not others? Decoding a reserved character changes the URL's meaning, so frameworks differ in when, or whether, they decode it inside path segments. Test the exact framework you deploy.
Decompose and build, don't concatenate
Nearly every URL bug above comes from treating URLs as strings you can glue together. Don't. To understand any URL — yours or a third party's — drop it into a URL parser and read the labelled parts, including the port and fragment you might otherwise miss. When a value needs to go into a path or query, use your language's component encoder rather than guessing which characters are legal where. When you're assembling a URL with parameters in code, use a builder such as URL plus URLSearchParams, which handles the separators so you don't ship the &-vs-? bug that string concatenation invites. Treat URLs as structured data, not text, and most of this class of bug disappears.