Sub topic 2
Anatomy of a domain name
This chapter takes a domain name apart and names each piece. You will learn the vocabulary that the rest of the tutorial relies on: label, TLD, registrable domain, subdomain, apex, and fully qualified domain name (FQDN).
It assumes you know what a website and a browser are, that you can run commands in a terminal, and that you know an IP address is a number that identifies a machine on a network. The previous chapter covered why names exist. This one covers how a name is written.
Names are read from right to left
A domain name is a list of labels separated by dots. A label is one segment of the name. The name www.example.com has three labels: www, example, and com.
Humans read the name left to right, but DNS (the Domain Name System) treats it as a path through a tree, read from right to left. The rightmost label is the broadest. Each label to its left narrows the scope, much like a postal address that goes from country to city to street.
A complete name has one more label that people usually leave out: the empty root label after the final dot. Written in full, the name is www.example.com. with a trailing dot. That trailing dot says the name is complete and nothing more should be added to it.
Fully qualified and relative names
A fully qualified domain name (FQDN) names one exact spot in the tree. It includes every label up to the root. www.example.com. is an FQDN.
A relative name is incomplete, and software must finish it by adding a suffix. You meet relative names in two places:
- Zone files, where names without a trailing dot get the
$ORIGINappended. - Your operating system's search domain, which can turn
intranetintointranet.corp.example.com.
Browsers and most tools hide the trailing dot, so example.com and example.com. normally behave the same. Inside a zone file the dot decides the meaning:
$ORIGIN example.com.
$TTL 3600
; Correct: the trailing dot makes the target absolute.
www IN CNAME example.com.
; Wrong: the origin is appended, giving example.com.example.com.
blog IN CNAME example.comThe second line is one of the most common zone-file mistakes. The record loads without any error, and then the alias points at a name that does not exist.
The parts, from right to left
| Part | In `www.example.com.` | Meaning |
|---|---|---|
| Root | the final empty label | Top of the tree. Run by the root server operators. |
| Top-level domain (TLD) | `com` | The rightmost visible label. Managed by a registry. |
| Second-level domain | `example` | The label you register under the TLD. |
| Registrable domain | `example.com` | The name you buy. Also called the apex or base domain. |
| Subdomain | `www` | Any label placed to the left of the registrable domain. |
| FQDN | `www.example.com.` | The complete name, ending at the root. |
Top-level domains
TLDs fall into a few groups:
- Generic TLDs (gTLDs) such as
com,org,net, and hundreds of newer ones such asappordev. - Country-code TLDs (ccTLDs), two letters, such as
de,jp, oruk. Each has its own registration rules. Some require a local presence. - Sponsored TLDs such as
eduandgov, which are restricted to qualified organizations.
The arpa TLD is reserved for infrastructure. For example, reverse lookups from an IP address back to a name live under in-addr.arpa.
The registrable domain and the public suffix
You usually register the label directly below a TLD. Some registries add a layer in between. In the UK, many names live under co.uk, so you register example.co.uk and not example.uk. The part you cannot register, here co.uk, is a public suffix. The part you can register is the registrable domain.
You cannot find this boundary by counting dots. example.com has two labels and example.co.uk has three, yet both are registrable domains. Browsers and security tools use the community-maintained Public Suffix List to decide where the boundary falls. They use it to stop a site on a.co.uk from setting cookies that apply to b.co.uk.
Subdomains and the apex
A subdomain is any name below a domain you control. www, mail, and api are labels that become subdomains of example.com. They can be nested, as in eu.api.example.com.
The registrable domain itself is called the apex, the root domain, or the naked domain. The word "root" here is confusing, because it has nothing to do with the DNS root. This tutorial says "apex" to avoid the clash.
Technically, a subdomain is just another name in the same tree. It has no special type. The practical differences come from DNS rules about what you can put at each name:
- The apex must carry certain records, such as the SOA and NS records covered later, and a CNAME alias is not allowed there.
- A subdomain can point to a different server from the apex.
- A subdomain can be delegated, meaning its administration is handed to a different set of name servers. That is how a large organization lets one team run
cloud.example.comindependently.
You can create as many subdomains as you like at no extra registration cost. You pay to register the domain, not for the labels beneath it.
Rules for what a label can contain
The original rules, often called "letters, digits, hyphen" (LDH), still govern the names used for hosts and websites:
- A label is 1 to 63 characters long.
- The full name is at most 253 characters in text form.
- Allowed characters are the letters
atoz, the digits0to9, and the hyphen. - A label cannot start or end with a hyphen.
- Matching is case-insensitive.
Example.COMandexample.comare the same name. - A label cannot be empty, so
a..bis invalid.
Underscores break the host-name rule, yet you will see them in labels like _dmarc.example.com. DNS itself allows nearly any bytes in a label. The LDH restriction applies to names of hosts. Underscore labels mark service-specific data and are used with TXT and other record types.
Non-English names
Names such as bücher.example are internationalized domain names (IDNs). DNS cannot carry non-ASCII characters in labels directly. Software converts each such label to an ASCII form called Punycode, which starts with the prefix xn--. DNS only ever sees the xn-- form, and the browser displays the readable version.
Checking names in code
This Python script checks a name against the LDH rules and shows the Punycode conversion. It uses only the standard library.
import re
LABEL = re.compile(r"^(?!-)[a-z0-9-]{1,63}(?<!-)$")
def check_fqdn(name: str) -> list[str]:
"""Return the labels of a name, or raise ValueError if it breaks LDH rules."""
name = name.lower().rstrip(".")
if len(name) > 253:
raise ValueError("name longer than 253 characters")
labels = name.split(".")
for label in labels:
if not LABEL.match(label):
raise ValueError(f"invalid label: {label!r}")
return labels
for candidate in ["WWW.Example.com.", "-bad.example.com", "a..b.com", "_dmarc.example.com"]:
try:
print(candidate, "->", check_fqdn(candidate))
except ValueError as err:
print(candidate, "->", err)
print("bücher.example".encode("idna"))Running it prints the three labels for the first name. The next three names are rejected: a leading hyphen, an empty label, and an underscore. The last line prints b'xn--bcher-kva.example', the wire form of the IDN. Note that the script rejects _dmarc only because it applies host-name rules. A DNS server would accept that label.
Common mistakes
- Treating
wwwas required. It is only a label. Many sites answer on the apex, and some have nowwwat all. You must configure each name you want to work. - Assuming
www.example.comandexample.comare the same site. They are two separate names. Each needs its own record, and a visitor can reach one while the other fails. - Forgetting the trailing dot in zone files. See the example earlier in this chapter.
- Confusing the domain with the URL. In
https://www.example.com/shop?x=1, the domain name is onlywww.example.com. The scheme, path, and query belong to HTTP, not DNS. - Mixing up "domain" and "hostname". In everyday use, a hostname is a name that points to a machine, such as
www.example.com. The domain is the zone it belongs to,example.com. The terms overlap, andexample.comcan be both.
Exercise
For the name shop.eu.example.co.uk., list every label from right to left, identify the public suffix and the registrable domain, and state which labels are subdomains.
Show answer
Labels from right to left: the empty root label, uk, co, example, eu, shop. The public suffix is co.uk. The registrable domain is example.co.uk. The subdomain labels are eu and shop, so eu.example.co.uk is a subdomain and shop.eu.example.co.uk is a subdomain of that.
Exercise
A zone file for example.com contains the line docs IN CNAME hosting.example.net with no trailing dot. What name does the alias really point to, and how do you fix it?
Show answer
The $ORIGIN is appended, so the alias points to hosting.example.net.example.com., which probably does not exist. Fix it by adding a trailing dot: docs IN CNAME hosting.example.net.