Bookmarks

URL — URL

Uniform Resource Locator

A string a program uses to find a resource on a network — a page, a file or an API endpoint; it consists of a scheme, a host and a path, often with a port, a query or a fragment.

Also: URL, web address, web address, URL

checked 9 Nov

All terms

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:

text
https://example.com:8443/docs/search?q=url&lang=ru#resultsscheme  host        port path        query         fragment
PartExampleNote
schemehttpsrequired; mailto and file work without a host
hostexample.coma domain or an IP address
port:8443optional; the default depends on the scheme
path/docs/searchoptional; segments are separated by a slash
query?q=url&lang=ruoptional; introduced by a question mark
fragment#resultsoptional; 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: %20 instead 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ß.example is written as xn--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.

text
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 page

The 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

jsParsing and building an address in JavaScript
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 pages

Verified 9 Nov