Networking

What is CNAME record?

A CNAME (Canonical Name) record is a type of Domain Name System (DNS) resource record that maps one domain name (the alias) to another domain name (the canonical name).

What is a CNAME record?

A CNAME (Canonical Name) record is a type of Domain Name System (DNS) resource record that maps one domain name (the alias) to another domain name (the canonical name). This allows you to create alternative names or aliases for a website or web service, redirecting traffic from the alias domain to the canonical domain.

How CNAME records work

When a client (such as a web browser) requests a domain name, the DNS system looks up the corresponding IP address. If the domain has a CNAME record, the DNS server will instead return the canonical domain name specified in the CNAME record.

For example, if the domain www.example.com has a CNAME record pointing to example.com, a request for www.example.com will be redirected to example.com. The client's browser will then make a request to the IP address associated with example.com.

CNAME records are commonly used in the following scenarios:

  • Subdomain aliases: Creating a subdomain (like www.example.com) that points to the main domain (example.com).
  • CDN/load balancing: Mapping a domain to a content delivery network (CDN) or load balancer for improved performance and redundancy.
  • Vanity domains: Providing a user-friendly domain name that redirects to a longer, more complex URL.
  • Migrating domains: Transitioning a website to a new domain while preserving the old domain as an alias.

CNAME record best practices

When using CNAME records, it's important to keep a few best practices in mind:

  • Avoid using CNAME records for root domains: CNAME records should not be used for the root domain (e.g., example.com) as this can cause issues with other DNS records like MX (mail) records. Instead, use A or AAAA records for the root domain.
  • Limit the number of CNAME redirects: Each CNAME lookup adds latency, so try to minimize the number of CNAME redirects to improve website performance.
  • Monitor CNAME configurations: Regularly review your CNAME records to ensure they are still pointing to the correct canonical domain, especially when making changes to your infrastructure or domain setup.
  • Consider SEO implications: Search engines may treat CNAME redirects differently than direct A or AAAA records, so be mindful of how CNAME usage could impact your website's SEO.

Real-world CNAME record examples

Here are a few examples of how CNAME records are used in the real world:

The domain www.example.com has a CNAME record pointing to example.com. This allows users to access the website using either the www subdomain or the root domain.
The domain cdn.example.com has a CNAME record pointing to a content delivery network (CDN) provider's domain, such as example.cloudfront.net. This allows the website to leverage the CDN's global infrastructure for faster content delivery.
The domain mysite.com has a CNAME record pointing to the canonical domain example.com/mysite. This provides a user-friendly, branded domain name that redirects to the actual website location.

Conclusion

CNAME records are a versatile DNS feature that allow you to create aliases for your domains, enabling a wide range of use cases from subdomain management to content delivery optimization. By understanding how CNAME records work and following best practices, you can leverage this powerful DNS capability to improve the user experience and performance of your web applications.

Studying for CompTIA (Networking)?

ExamWizardz turns the official objectives into a guided study plan — with practice tests, real PBQs, and a readiness score. Join the waitlist to be first in when CompTIA A+ launches.