INTERNET FUNDAMENTALSAI-REVIEWED

What is a URL?

Useful first: Domain names · optional, not a requirement

Choose your depth

Stay with the essentials, or look closer.

Simple explanation
2 min read · estimate

YOUR GOALSeparate the website host from the resource path.

Read a web address

An address for something on the web

A URL is an address that identifies a resource. That resource might be a page, an image, or another kind of content. This article uses ordinary HTTP and HTTPS web addresses; other URL schemes exist too.

Consider the fictional address https://learn.example/guides/urls. The scheme is https, the hostname is learn.example, and the path is /guides/urls. A domain name is only one part of this complete address.

[1] [6]

An address is not the content itself

Changing the path to /images/map.svg gives a different address on the same host. A path can look like folders and a filename, but a server may generate the response instead of reading that exact file from disk.

Knowing an address does not guarantee the resource still exists or that you can access it. The address describes where to ask; the response tells you what is available.

[1]
THE IDEA TO KEEP

Read the host and the path separately when looking at a web address.

ONE THING TO WATCH OUT FOR

The domain name and the full URL are the same thing.

In this web address, the domain identifies the host; the URL also includes a scheme and a path, and may include other parts.

A QUICK CHECK, NOT A TEST
In https://learn.example/guides/urls, which part is the hostname?
Show the explanation

learn.example learn.example is the hostname. The path selects the resource, while https is the scheme.

Progress is saved only in this browser.

KEEP EXPLORINGDomain names
Was this explanation clear?
Report a mistake by email
Basic explanation
2 min read · estimate

YOUR GOALIdentify what belongs to a request and resolve a link using its base.

Queries, fragments and relative links

A query carries application-defined information

In https://learn.example:8443/guides/urls?mode=basic#quiz, 8443 is an explicit port. Without an explicit port, HTTPS uses its default port, 443.

The query begins after ? and ends before #, if a fragment follows. Here it is mode=basic. A site could use it to choose a reading mode. Name/value pairs are common, but the application defines their meaning: a query is not automatically a search or a database command.

[1] [2] [6]

A fragment is handled separately

The fragment #quiz can point to an element in the returned document. For a direct request to this HTTPS server, the path and query are /guides/urls?mode=basic; #quiz is excluded from that request target.

That is not a secrecy feature. Page JavaScript can read an ordinary fragment through location.hash. Do not assume that information becomes private just because it follows #. For the broader request/response exchange, see HTTP.

[3] [8]

Relative links depend on the base

A relative reference needs a base URL. With https://learn.example/guides/urls/ as the base, images/map.svg resolves to https://learn.example/guides/urls/images/map.svg.

Remove the base URL's final slash and the same reference resolves to https://learn.example/guides/images/map.svg. Resolution uses the directory up to the last slash. A root-relative reference, /images/map.svg, resolves to https://learn.example/images/map.svg with either base. This is URL resolution, not a guarantee about server folders.

[4]
THE IDEA TO KEEP

Keep query and fragment roles distinct, and include the base when interpreting a relative link.

ONE THING TO WATCH OUT FOR

A query and a fragment are interchangeable extra text.

A query is part of the requested address; a fragment is excluded from the resource request and handled separately on the client.

A QUICK CHECK, NOT A TEST
For a direct HTTPS request to https://learn.example/guides/urls?mode=basic#quiz, which path and query are sent in the request target?
Show the explanation

/guides/urls?mode=basic The query remains part of the request target. The fragment beginning with # is excluded.

Progress is saved only in this browser.

KEEP EXPLORINGDomain names
Was this explanation clear?
Report a mistake by email
Advanced explanation
3 min read · estimate

YOUR GOALUse URL APIs to resolve addresses and encode query values correctly.

Keep data separate from delimiters

Encoded bytes can be data instead of structure

Percent encoding writes a byte as % followed by two hexadecimal digits. A literal # starts a fragment, while %23 can represent that character as data in a path.

For example, https://learn.example/notes%23draft has pathname /notes%23draft and no fragment. In https://learn.example/notes#draft, the pathname is /notes and the fragment is #draft. Decoding the entire address before parsing it can change its structure. How a server maps an encoded path to a resource is a separate application decision.

[5] [6]

Let the URL parser resolve the address

The browser URL API follows the WHATWG URL standard. Use new URL(reference, base) to resolve a relative reference instead of joining arbitrary strings.

const address = new URL(
  "../http?mode=basic#quiz",
  "https://learn.example/guides/urls/"
);
address.href;
// https://learn.example/guides/http?mode=basic#quiz

This constructs and parses an address; it does not fetch the page or prove the destination exists.

[4] [6]

Encode query values once

For a query that uses name/value pairs, supply the original value to searchParams.set():

const address = new URL("https://learn.example/search");
address.searchParams.set("q", "DNS & URLs");
address.href;
// https://learn.example/search?q=DNS+%26+URLs

The ampersand remains part of one value instead of separating parameters. URLSearchParams uses form encoding: a space becomes +, and a literal plus becomes %2B. Reading the value with get("q") returns DNS & URLs.

Do not pre-encode the value before setting it: a supplied %26 becomes %2526, preserving the percent sign as data. Also, + does not mean a space everywhere in a URL; this rule belongs to form-style query encoding.

[7] [6]
THE IDEA TO KEEP

Use a URL parser for structure and query APIs for individual parameter values.

ONE THING TO WATCH OUT FOR

Decode a whole URL first, then split it at its punctuation.

Decoding reserved characters can turn data into delimiters. Parse the URL structure first and handle individual components with their encoding rules.

A QUICK CHECK, NOT A TEST
You want q to contain the literal text DNS & URLs. What should you pass as the value to address.searchParams.set("q", value)?
Show the explanation

DNS & URLs Pass the original text. URLSearchParams serializes its spaces and ampersand; pre-encoded input would preserve the escape text instead of the intended value.

Progress is saved only in this browser.

KEEP EXPLORINGDomain names
Was this explanation clear?
Report a mistake by email
GO STRAIGHT TO THE SOURCE

Keep asking. Check the details.

Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.

No human review is claimed. AI review can miss errors; check the original sources below.

  1. MDN — What is a URL?
  2. MDN — URI query
  3. MDN — URI fragment
  4. MDN — Resolving relative references to a URL
  5. RFC 3986 — Percent encoding and reserved characters, sections 2.1–2.4
  6. WHATWG — URL parsing and URL API standard
  7. MDN — URLSearchParams encoding and plus signs
  8. MDN — Location.hash
How explanations are prepared