Sub topic 2
Anatomy of a domain name
This chapter assumes you know what a website and a browser are, can use a command line, and know that an IP address identifies a machine on a network. Here you learn to read a domain name from right to left and to name each part correctly. Later chapters use these terms constantly.
Labels and dots
A domain name is a sequence of labels separated by dots. A label is one piece of text between dots. Each label can be up to 63 characters long. The whole name can be up to 253 characters in its text form. Labels may contain letters, digits, and hyphens. A label cannot start or end with a hyphen. Case does not matter, so Example.COM and example.com are the same name.
Take the name www.shop.example.com. It has four labels: www, shop, example, and com.
Reading right to left
DNS treats a name as a path through a tree. The most general part is on the right, and each label to the left is more specific. This is the opposite of a file path like /usr/local/bin, which starts at the root on the left.
| Label | Position | Name for this level |
|---|---|---|
| (empty) | Far right, after the last dot | Root |
| `com` | Rightmost visible label | Top-level domain (TLD) |
| `example` | Next to the left | Second-level domain |
| `shop` | Next | Subdomain of `example.com` |
| `www` | Leftmost | Subdomain of `shop.example.com` |
The TLD is the last label, such as com, org, or de. Some TLDs are country codes (uk, jp), and others are generic (com, dev). The name you register with a registrar is usually the second-level domain plus its TLD, here example.com. This is often called the registered domain or apex domain.
A subdomain is any name below a domain you control. You create subdomains yourself, without paying a registrar. Whoever controls example.com can add shop.example.com or a.b.c.example.com at any time.
The trailing dot and FQDNs
A fully qualified domain name (FQDN) names one exact place in the tree, with nothing left to guess. Written in full, it ends with a dot that stands for the root:
www.shop.example.com.Most tools let you leave the final dot off. Zone files are different. In a zone file, a name without a trailing dot is treated as relative and has the current origin added to it. Writing mail.example.com without a dot inside the zone for example.com would produce mail.example.com.example.com.. This is a common mistake.
$ORIGIN example.com.
www IN A 192.0.2.10Here www is relative, so it means www.example.com.. The address 192.0.2.10 is a reserved documentation address.
Some TLDs have more than one level
Not every registered domain has exactly two labels. Some country TLDs reserve a second level for registrations. For example.co.uk, the registered domain is example.co.uk, and co.uk is the registry's own suffix. Software that needs to find the registered domain, such as a cookie handler or a certificate tool, relies on the Public Suffix List. It is a maintained list of suffixes under which anyone can register a name. Splitting on the last two dots is wrong for these cases.
name = "www.example.co.uk"
labels = name.split(".")
print(labels)
print(len(labels), "labels")
print("TLD:", labels[-1])This code prints the label list, the count, and the last label. It shows uk as the TLD, but it cannot tell you where the registered domain begins. That needs the Public Suffix List.
Internationalized names
Names in other scripts are stored in DNS using only ASCII. A label such as münchen is encoded as xn--mnchen-3ya. The xn-- prefix marks the Punycode form. Browsers show you the readable form, and DNS carries the encoded form.
Common errors
- Treating
wwwas part of the registered domain. It is only a conventional subdomain label. - Forgetting the trailing dot in zone files, which doubles the origin.
- Assuming the last two labels are always the registered domain.
- Using labels longer than 63 characters or names longer than 253.
Exercise
For the name blog.docs.example.org., identify the root, the TLD, the second-level domain, and every subdomain label. What goes wrong if you write docs.example.org without a trailing dot inside the zone file for example.org?
Show answer
The root is the empty label after the final dot. The TLD is org. The second-level domain is example, so the registered domain is example.org. The subdomain labels are docs and blog, where blog is the most specific. Without a trailing dot, a zone file treats the name as relative and appends the origin, giving docs.example.org.example.org., which is a different and wrong name.