What is a CNAME Record?
A CNAME record (Canonical Name record) is a type of DNS record that maps an alias name to a true or canonical domain name. CNAME records are typically used to map a subdomain such as www or mail to the domain hosting that subdomain's content.
Structure of a CNAME Record
www.example.com. 3600 IN CNAME example.com.
- Host: The alias (e.g.,
www). - TTL (Time to Live): How long (in seconds) the record should be cached.
- Type:
CNAME. - Value: The canonical domain name it points to.
Crucial Restriction: The Apex Limitation
According to DNS RFCs, a CNAME record cannot coexist with any other record for the same name. Because the root (apex) of a domain (e.g., example.com) must have SOA and NS records, you cannot have a CNAME record at the root of a domain.
Solution: Use an ALIAS or ANAME record if your DNS provider supports it.
How CNAME Resolution Works
When a DNS resolver finds a CNAME record, it restarts the lookup process using the canonical name instead of the original name.
- User requests
www.mysite.com. - Resolver finds
www.mysite.com CNAME mysite.com. - Resolver then looks up
mysite.com(the canonical name). - Resolver finds
mysite.com A 1.2.3.4and returns the IP to the user.
Common Use Cases
- Pointing www to Root: Mapping
www.example.comtoexample.com. - Cloud Services: Pointing your domain to a service like Heroku, Shopify, or a CDN (e.g.,
mysite.github.io). - Traffic Management: Moving services between different hostnames without changing client configurations.