A URL is the address of a page, a file or an API endpoint. A browser splits it into parts, finds the server and requests the resource; developers write the same strings in code, configs and documentation. The term became established in IETF documents in 1994, while the web has used this form of address since 1990.
What a URL consists of
Read left to right, an address goes like this: the scheme names the access method, the host the server, the port the number on that server (443 by default for HTTPS and 80 for HTTP, in which case it is omitted), and the path the address of the resource within the server. The query after the question mark passes parameters as “key=value” pairs, and the fragment after the hash points to a place inside the document; it never reaches the server:
https://example.com:8443/docs/search?q=url&lang=ru#resultsscheme host port path query fragment| Part | Example | Note |
|---|---|---|
| scheme | https | required; mailto and file work without a host |
| host | example.com | a domain or an IP address |
| port | :8443 | optional; the default depends on the scheme |
| path | /docs/search | optional; segments are separated by a slash |
| query | ?q=url&lang=ru | optional; introduced by a question mark |
| fragment | #results | optional; introduced by a hash, not sent to the server |
Which characters are allowed
- Any URL may contain Latin letters, digits and the characters
-,.,_,~as they are; everything else is written with percent-encoding:%20instead of a space. - The percent sign itself is encoded as
%25, so a%in an address is always the start of an encoded byte. - Cyrillic and other non-ASCII characters are encoded as UTF-8 bytes: the word
словоin a query becomes%D1%81%D0%BB%D0%BE%D0%B2%D0%BE. - A non-Latin domain is stored in punycode:
faß.exampleis written asxn--fa-hia.example, while the browser shows the original form in the address bar. - A username and password in an address are legal, but writing them there is discouraged: addresses end up in bookmarks, history and logs.
- The standards set no length limit: the server decides which addresses it accepts, and replies with code 414 URI Too Long if an address is too long.
URL and URI
A URL is a kind of URI. A URI identifies a resource, while a URL also describes where it is and how to get it: this is how RFC 3986 separates the terms, calling a URL a subset of URIs. An example of a URI without an address is urn:isbn:9780307476463: the book is named, but nothing says where to get it. The reverse always holds: every URL is a URI.
urn:isbn:9780307476463 — a URI but not a URL: a name without an addressmailto:admin@example.com — a URL with the mailto scheme//example.com/img.png — a relative address: the scheme is taken from the current pageThe current web standard, the WHATWG URL Standard, recommends simply writing URL everywhere: in practice one algorithm parses both URIs and URLs, and it regards distinguishing them as a source of confusion. The classic RFC 3986 syntax is still alive, though: it describes the general grammar for all schemes.
URLs in code
const url = new URL("https://example.com:8443/docs/search?q=url&lang=ru#results");url.host; // "example.com:8443"url.pathname; // "/docs/search"url.searchParams.get("lang"); // "ru"url.hash; // "#results" new URL("/docs", "https://example.com").href; // "https://example.com/docs"The URL constructor throws an exception on an invalid string, while the static method URL.canParse() checks an address without exceptions. A relative address is accepted only with a second argument, the base from which the missing parts are filled in.
Sources
the term is written from these pagesVerified 9 Nov