A CNAME record works as an alias for domain names.
What is a DNS CNAME record?
A CNAME record (Canonical Name Record) allows a subdomain to point to (or "redirect" to) another domain known as the “canonical” domain (or actual domain name). The subdomain thus becomes an alias, or synonym, for the domain to which it points.
A CNAME record can be used to reference a domain alias instead of its actual name.
CNAME records generally point to an A record (with an IP address in IPv4 format) or an AAAA record (IPv6).
A CNAME record is used instead of an A record when a subdomain is an alias for another domain. All CNAME records must point to a domain, never to an IP address.
A CNAME record cannot be created for a domain.
Here is an example of a CNAME record:
example.alias.com. 86400 IN CNAME domain.com.
domain.com. 86400 IN A 195.64.164.87
| example.alias.com | record type | value | TTL |
|---|---|---|---|
| @ | CNAME | is an alias for domaine.com | 86400 |
In this example, you can see that example.alias.com points to domain.com, and as we know that it is based on the A record for domain.com, we can therefore conclude that it will ultimately resolve to the IP address 195.64.164.87.
This means that when a DNS server looks up the DNS records for example.alias.com, it will actually perform another DNS lookup for domain.com, returning the IP address from the A record for domain.com. In this case, domain.com is said to be the “canonical” name (or actual domain name) of example.alias.com.
The advantage is that entering https://example.alias.com or https://domain.com takes you to the same website, hosted on the server whose IP is 195.64.164.87 in our example.
If the IP of the server hosting the website changes, the modification only needs to be made once, to the A record for domaine.com, by updating the IP address.
Often, when websites have subdomains such as blog.domain.com or shop.domain.com, these subdomains have CNAME records pointing to the root domain domain.com. This way, if the host's IP address changes, only the DNS A record for the root domain needs to be updated, and all CNAME records will follow any changes made to the root.
In our example, simply replace the IP address 195.64.164.87 in the A record for domain.com with the new address 195.64.164.188 to also update the CNAME and therefore the destination of example.alias.com:
example.alias.com. 86400 IN CNAME domain.com.
domain.com. 86400 IN A 195.64.164.188
However, a CNAME record does not necessarily have to resolve to the same website as the domain to which it points. The CNAME record only points to the same IP address as the “canonical” domain. Once the IP address is reached, the web server will still process the URL accordingly. Thus, for example, blog.domain.com or shop.domain.com can have a CNAME pointing to domain.com, directing the visitor to the IP address of domain.com. However, when the browser actually connects to this IP address, the web server examines the URL, sees that it is blog.domain.com or shop.domain.com, and then provides the page corresponding to the subdomain — the blog or online shop — rather than the homepage of the domain.com website.
For example:
blog.domain.com 33200 IN CNAME domain.com.
shop.domain.com 33200 IN CNAME domain.com.
| blog.domain.com | record type | value | TTL |
|---|---|---|---|
| @ | CNAME | is an alias for domain.com | 86400 |
| shop.domain.com | record type | value | TTL |
|---|---|---|---|
| @ | CNAME | is an alias for domain.com | 86400 |
Why use a CNAME record?
A CNAME allows you to point an alias subdomain to the URL of its root domain or another domain.
For example:
blog.example.com 33200 IN CNAME yourblog.wordpress.com.
www.example.com 33200 IN CNAME yourblog.wordpress.com.
| blog.example.com | record type | value | TTL |
|---|---|---|---|
| @ | CNAME | is an alias for yourblog.wordpress.com | 86400 |
| www.example.com | record type | value | TTL |
|---|---|---|---|
| @ | CNAME | is an alias for yourblog.wordpress.com | 86400 |
The CNAME records above redirect visitors to your WordPress (yourblog.wordpress.com) when they use the URLs blog.example.com and www.example.com, in addition to the main URL yourblog.wordpress.com.
When should you not use a CNAME record?
A CNAME record cannot be created for a domain. The record must contain a subdomain. A CNAME record cannot be created at the apex of a zone. Learn more.
NB: Do not confuse a CNAME with a web redirect (URL or IP), an ANAME or ALIAS, or a DNAME.
Restrictions: A CNAME record cannot coexist with a record of another type on a subdomain. This means, in particular, that a CNAME cannot be created for the domain root, since there are already, at a minimum, NS records and an SOA record. The CNAME record conflicts with records found at the apex of a zone, such as the SOA and NS records. In addition, MX and NS records cannot point to a CNAME record; they must point to an A record (IPv4) or an AAAA record (IPv6).
Can a CNAME record point to another CNAME record?
Yes, this is possible but not recommended. Pointing a CNAME record to another CNAME record is inefficient, as it involves several DNS lookups before the website can be loaded, which slows down resolution and therefore negatively impacts browsing and the user experience. This configuration adds at least one additional step to the DNS query process and should therefore be avoided where possible to optimise resolution and the user experience.
Conclusion:
• A CNAME record redirects a subdomain to a domain but cannot be created at the root of your domain;
• A CNAME record can simplify DNS management by allowing, for example, a single modification to the canonical name record to which the CNAME points when the IP changes;
• A CNAME record must point to a domain / subdomain and cannot point to an IP;
• A CNAME record cannot be created for a domain;
• A CNAME record must contain a subdomain;
• A CNAME record cannot coexist with a record of another type on a subdomain;
• A CNAME record should not point to another CNAME record;
• MX and NS records must not point to a CNAME.