Chapter 3 · Lesson 16 of 22
Domains and HTTPS

Subdomains and wildcard DNS records explained

A subdomain is a name below your domain with its own DNS records, and a wildcard record (*.example.com) answers for names that have no explicit record at that level.

By the end you will

Create subdomain records, add a wildcard where it fits and predict which names it will and will not answer.

Time
10 min
Level
Beginner
Check
4 questions
You need
  • A domain whose DNS you can edit
  • dig installed

A subdomain is a name placed in front of your domain, such as app.example.com or api.example.com. It exists when a record for that name exists in your DNS zone. A wildcard record, written with an asterisk as its name, answers for names that have no record of their own. Both are ordinary records, and the choice between many explicit records and one wildcard depends on how predictable your names are.

How it works#

Names in DNS are made of labels separated by dots. In app.example.com the labels are app, example and com. A subdomain record is just a record whose name has extra labels in front of the zone name. In most provider panels you type only the part in front of the domain: entering app for a record in the example.com zone creates app.example.com.

A wildcard is a record whose leftmost label is *. When a resolver asks the authoritative server for a name that does not exist, the server checks whether a wildcard applies and, if so, answers as though the record existed under the requested name. The rules are defined in RFC 4592, and they are narrower than many people assume.

Step 1: Add a record per subdomain#

The explicit approach lists each name. For two apps on one server:

output
app.example.com.   300  IN  A     203.0.113.10
api.example.com.   300  IN  A     203.0.113.10

You can also use a CNAME so the subdomain follows another name:

output
docs.example.com.  300  IN  CNAME app.example.com.

Explicit records are easy to audit, and each can point somewhere different or have its own TTL. The cost is that every new name needs an edit. See DNS record types explained for choosing A, AAAA or CNAME.

Step 2: Add a wildcard when names are open ended#

If a reverse proxy routes by hostname and you create names often, one wildcard saves edits:

output
*.example.com.     300  IN  A     203.0.113.10

Now anything.example.com, test.example.com and a typo like wwww.example.com all resolve to 203.0.113.10. That is convenient for per-customer or per-preview subdomains, and it also means every typo resolves to your server, so the server must handle unknown hostnames sensibly (return a 404 or close the connection).

Step 3: Know what a wildcard does not match#

Four points cause most confusion.

The apex is not covered. *.example.com does not answer for example.com itself. Add an explicit record (an A record, or an ALIAS where your provider supports it, see CNAME flattening and ALIAS records).

Explicit records win. If api.example.com has its own record, the wildcard is ignored for api.example.com, for every record type that explicit name holds. This also means an explicit name with only a TXT record can stop the wildcard A record from answering for it, because that name exists and has no A record. Answering “no data” is correct behavior there.

Deeper levels are not covered the way people expect. The wildcard stands in for one label at the position of the asterisk. A lookup for a.b.example.com can match *.example.com only if b.example.com does not exist as a name in the zone. If anything exists at or below b.example.com, for instance c.b.example.com, then b.example.com is an “empty non-terminal” that exists, and the wildcard no longer applies to names under it. To cover a second level you add a second wildcard such as *.b.example.com.

The wildcard matches by record type too. A wildcard A record answers A queries only. If you want IPv6, add a wildcard AAAA record next to it.

Step 4: Think about the certificate#

DNS and TLS are independent. Making every subdomain resolve does not make HTTPS work for it. A certificate must cover each name. You can request one certificate per hostname, or a single wildcard certificate for *.example.com, which has the same one-level limit as the DNS wildcard and also does not cover the apex. Wildcard certificates require the DNS-01 challenge. Read Wildcard certificates explained and How TLS certificates are issued.

Check it worked#

Query names that should and should not match:

bash
dig +short app.example.com A
dig +short random-name.example.com A
dig +short example.com A
dig +short a.b.example.com A

The first is explicit, the second should return the wildcard target, the third should return your apex record (not the wildcard), and the fourth shows whether the deeper name is covered. Use dig @ns1.example.net to rule out caching, as described in Check DNS with dig.

Edge cases and gotchas#

  • Typos resolve: with a wildcard, mistyped names reach your server, so unknown hosts need a safe default response.
  • Email and wildcards: a wildcard A record does not create mail routes, and a wildcard MX can affect more names than you intended.
  • CNAME wildcards: a wildcard CNAME is allowed in most providers, but chains of aliases add lookup time.
  • Provider quirks: some control panels display the wildcard as * and others as *.example.com, so check the final record in dig.
  • Security: a wildcard makes subdomain takeover and phishing lookalikes easier if a stray name points to something you no longer control.
  • Negative caching: a name that did not exist a minute ago may be cached as missing even after you add the wildcard.

Troubleshooting#

SymptomLikely causeFix
example.com does not resolve but sub.example.com doesThe wildcard does not cover the apexAdd an explicit apex record
a.b.example.com returns NXDOMAINb.example.com exists, so the wildcard no longer appliesAdd *.b.example.com or an explicit record
One subdomain ignores the wildcardIt has its own record of another typeAdd the missing record type to that name
HTTPS fails on a subdomain that resolvesThe certificate does not cover that nameIssue a certificate that includes it
New subdomain not found for a whileNegative cache from an earlier lookupWait for the SOA minimum, test the authoritative server
Any random name opens your siteWildcard plus a catch all virtual hostAdd a default server block that returns 404

Quick reference#

output
app     A      203.0.113.10     -> app.example.com
*       A      203.0.113.10     -> any other name directly below example.com
@       A      203.0.113.10     -> example.com itself (apex)

Next#

Continue with CNAME flattening and ALIAS records.

Check what you learned4 questions
1Does a wildcard record *.example.com answer for example.com itself?
2A wildcard *.example.com and an explicit record for api.example.com both exist. Which answers for api.example.com?
3Does *.example.com match a.b.example.com?
4Which record type creates a subdomain that points at an IPv4 address?

Frequently asked questions

How do I create a subdomain?

Add a record in your domain's DNS zone with the subdomain as the name, for example an A record named app pointing to your server IP. The subdomain exists as soon as the record does.

What does a wildcard DNS record do?

A record named * answers queries for any name under the domain that has no explicit record. For example *.example.com can send any random subdomain to the same server.

Does a wildcard DNS record cover the root domain?

No. The apex example.com needs its own A or ALIAS record. The wildcard only matches names below it.

Do I need a wildcard certificate for a wildcard DNS record?

DNS and certificates are separate. A wildcard record lets any subdomain resolve, but each name still needs a certificate that covers it, either one per name or one wildcard certificate.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios