A Canonical Name Record (CNAME record for short) is a DNS record that is used to alias a domain name to a different domain name. The domain which is referred to as the canonical name in a CNAME record is the domain on the right-hand side. This domain is the actual canonical name record which resolves information such as IP addresses, sub-domains, etc.
The canonical name record becomes useful when you want to direct users to the same information delivered on a separate url. For example, for website owners who use a domain such as
yourwebsite.com without any subdomain, a canonical name can be used to add www to the beginning of the URL. Therefore, when a user accesses your site via
www.yourwebsite.com, they will see the same information as if they accessed it via
Differences Between an A and CNAME Record
It is important to know the difference between CNAME and A records and when it is appropriate to use each. The main difference between these two records is that the A record maps a name to an IP address whereas a canonical name record maps a domain name to another domain name. For example:
Within the A record for the example above, example.com points to the IP 18.104.22.168. The CNAME example however uses the www.example.com subdomain and inherits the resolution chain of the canonical name record which would result in:
www.example.com. CNAME example.com. example.com. A 22.214.171.124
CNAME Record Limitations
According to the RFC1912, section 2.4, CNAME records have limitations such as:
- CNAMEs cannot point to an IP address
- CNAMEs cannot coexist with any other data since they cannot both be an alias and something else
- Both NS and MX records should never map to a CNAME alias. The following configuration should not be used:
example.com. MX 0 mailhost. mailhost. CNAME host.example.com. host.example.com. A 126.96.36.199
Although these limitations partly restrict the usability of canonical name records, there are other methods that can be used to overcome these restrictions.
CNAME Usage With The Apex of a Domain
Based on the limitations mentioned in the section above, a canonical name record cannot be used for aliasing the root domain due to its inability to coexist with other data. However, there do exist some solutions that help overcome this issue.
For example, DNS providers such as NS1, DNS Simple, and Cloudflare have each implemented their own version of what is called an Alias Record. An alias record essentially allows you to map the root domain to another service while still being able to coexist with other data.
This can prove useful for cases where you would like to map the root domain to a service such as Heroku or Github (e.g. example.com CNAME username.github.com) that resolve with a pool of IP addresses. Similarly, an Alias record can be used to point your root domain to a KeyCDN Zonealias.
Alias records work by masquerading the CNAME record as an A record. This allows you to stay RFC compliant, keeps the underlying A records in sync automatically, and allows the use of the root domain for other DNS records (e.g. MX records or NS records).
CNAME records are a very important and useful DNS record type. Having the ability to alias a custom domain to another service can prove useful whether it be for aesthetic purposes, usability, convenience, and so on. Although they do have some limitations, new methods (such as the ones mentioned above) are great for overcoming those limitations and using the canonical name to alias the root domain.