Multi-Level Subdomains: Can You Have a Subdomain of a Subdomain?
Yes. DNS names are a hierarchy, so api.staging.example.com is a perfectly valid name, a subdomain of staging.example.com, which is itself a subdomain of example.com. You create one the same way as any other subdomain: by adding a record for the full name in the zone that holds it, or by delegating a level to other name servers. The limits are generous: each label can be up to 63 octets and the whole name up to 253 characters in its usual text form. The catches are elsewhere, above all that a wildcard certificate for *.example.com covers only one level and will not secure api.staging.example.com.
How the names fit together
A DNS name is read right to left. In api.staging.example.com, com is the top-level domain, example is the registered domain, staging is a first-level subdomain and api is a second-level subdomain within it. There is nothing special about the second level in DNS itself: resolvers walk down the tree in the same way, as shown in the diagram of how a subdomain lookup resolves.
| Rule | Limit | Source |
|---|---|---|
| Length of one label | 63 octets | RFC 1035 |
| Length of a full name | 255 octets on the wire, usually 253 characters as text | RFC 1035 |
| Characters in host names | Letters, digits and hyphens, not starting or ending with a hyphen | Host name rules in RFC 952 and RFC 1123 |
| Non-ASCII labels | Encoded as Punycode xn-- labels | IDNA standards |
Creating the records
If example.com is hosted in one zone, you can add a record for the deeper name directly, with no need to create the middle level first:
| Name | Type | Value | TTL |
|---|---|---|---|
api.staging | A | 198.51.100.20 | 300 |
www.staging | CNAME | staging-host.example.net. | 300 |
The addresses and host names are documentation examples. Notice what happens to the middle name: with only these records, staging.example.com exists as a node in the tree but has no records of its own. Queries for it return an empty answer rather than an error, which surprises some people. Add an A or AAAA record at staging if the name itself should load something.
Delegating a level
Large organisations often delegate a level to a different team or provider, for example handing dev.example.com to a cloud DNS service with NS records, so that team can create anything.dev.example.com itself. Delegation keeps permissions tidy but adds a dependency: if the delegated name servers stop answering, every name below them fails. The comparison of delegating versus keeping names in the zone covers the trade-offs, and preventing subdomain takeover explains why abandoned delegations are a risk.
Certificates: the one-level rule
This is where multi-level names cause the most trouble. Under the rules for matching names in certificates, now set out in RFC 9525, which replaced RFC 6125 in 2023, a wildcard must be the whole of the left-most label and matches exactly one label. So:
*.example.comcoversstaging.example.comandshop.example.com;- it does not cover
api.staging.example.com, and it does not cover the bareexample.com; *.staging.example.comcoversapi.staging.example.combut notstaging.example.comitself.
The fix is to list the exact names, or the extra wildcard, as Subject Alternative Names on the certificate, or to issue a separate certificate for the deeper level. Wildcards from public certificate authorities such as Let's Encrypt require DNS-based validation; see issuing a wildcard certificate with DNS-01 and the wildcard versus SAN comparison.
Other things that change with depth
- Cookies. A cookie set for
.staging.example.comis visible to every name below it, and one set for.example.comto everything. See cookies across subdomains. - HSTS. An
includeSubDomainspolicy on a parent applies to every level beneath it, as HSTS includeSubDomains and preload explains. - Wildcard DNS records. A wildcard such as
*.example.comin DNS can match deeper names in some situations but not others, depending on what records exist at the intermediate level; test before relying on it, and see the Q&A on wildcard DNS records. - Readability. Users find long names hard to read and easy to fake. Keep depth for internal or technical names where it adds meaning.
Naming deeper subdomains well
A consistent pattern makes deeper names useful rather than confusing. Many teams put the environment or region in the middle label and the service on the left, as in api.staging.example.com and api.eu.example.com, so that a certificate wildcard or a delegation can cover one whole environment at a time. Keep labels short, lowercase and descriptive, avoid exposing internal project codenames in public DNS, and record every name in a subdomain register so that none are forgotten when a service is retired. The do's and don'ts for naming subdomains and naming subdomains for services and APIs go further.
Frequently asked questions
How many levels of subdomain can I have?
In practice, as many as fit within the 253-character limit for a full name, with each label 63 octets or fewer. Most real setups use one or two levels.
Do I need to create the middle subdomain first?
No. A record for a.b.example.com can exist without any record at b.example.com, unless you delegate b to other name servers.
Will my wildcard certificate cover a second-level subdomain?
No. A wildcard matches exactly one label. Add the deeper name or another wildcard to the certificate.
The SubDoms study hub lists more cards on records, certificates and security.