Skip to content

If you have ever set up a website, you have probably been told: "just add a CNAME for www". The advice is usually right, but it is rarely explained. A CNAME record is one of the simplest and most useful DNS record types, and also one of the easiest to misuse, because it comes with a rule that surprises beginners: it cannot sit on the root domain, and it cannot share a name with other records.

This guide explains what a CNAME is, how a resolver follows it, why www is its classic use case, where you must not use it and how to test the result. It builds on the idea of addresses described in how an A record points a domain to a server and its IPv6 sibling in the AAAA record guide, and it explains why email needs separate handling, which is covered in the MX record guide.

What a CNAME record is

CNAME stands for canonical name. A CNAME record says that one name is an alias for another name, called the canonical name. It contains no IP address. It contains another hostname.

For example, suppose www.example.com has a CNAME with the value example.com. When a resolver looks up www.example.com, the authoritative server answers: "that name is really example.com, go and ask about that." The resolver then looks up example.com, gets its address from the A or AAAA record, and returns the final result to the visitor. The mechanism is part of the original design of DNS in RFC 1034 and RFC 1035.

Alias, not redirect

A CNAME is not a web redirect. The visitor's browser still shows www.example.com in the address bar. The CNAME works inside DNS, only to decide which address to connect to. If you want the browser to jump from one address to another visibly, that is done by the web server with an HTTP redirect, not by DNS.

How a lookup follows a CNAME

  1. The visitor's resolver asks for www.example.com.
  2. The authoritative server replies with a CNAME pointing to the target name.
  3. The resolver asks for the target name, repeating the process.
  4. The target resolves to an A and possibly an AAAA record.
  5. The resolver returns the full chain to the client, which connects to the final IP address.

A chain can have more than one CNAME, such as www pointing to a platform hostname that itself points to another hostname. This works, but every extra step is another lookup that must succeed. Keep chains short, and avoid loops where two names point to each other, because the lookup will never finish.

The classic use: www

Most sites want both example.com and www.example.com to reach the same place. The usual pattern is:

Name Type Value Purpose
@ (root) A The server's IPv4 address The bare domain points to the server
@ (root) AAAA The server's IPv6 address, if used Same for IPv6
www CNAME example.com www follows whatever the root points to

The advantage is maintenance. If you later move to a different server and change the address of the root, www follows automatically, because it is only an alias. You edit one address instead of two.

Many hosting platforms also give you a hostname to use as the target, for example yoursite.platform-example.net. In that case, www is a CNAME to the platform's hostname, so the provider can change the underlying addresses without asking you to edit anything.

Why a CNAME cannot sit on the root domain

This is the rule that causes the most confusion. The root of your zone, written @, has to carry certain records, including SOA and NS. The DNS specification says that if a name has a CNAME, it must not have any other record data, because the CNAME says "this name is entirely an alias". Putting a CNAME on the root would conflict with the required SOA and NS records, and also with MX records for email.

So the root domain normally uses an A record, and an AAAA record if you support IPv6. Some DNS providers offer a feature often called alias, flattening or ANAME. It lets you point the root at a hostname and still behaves like an A record to the outside world. This is a provider feature, not standard DNS, so check how yours works and what it does when the target changes.

The one-name, one-CNAME-or-data rule

Remember the same restriction for any subdomain:

  • If blog.example.com has a CNAME, it can have no A, AAAA, MX or TXT record.
  • If you need a TXT record, such as a domain verification code, on a name that already has a CNAME, the verification must be placed elsewhere or done in a way the service allows.
  • If you need email on a name, give that name MX records, which means it cannot also be a CNAME.

Step by step: create a CNAME for www

  1. Decide the target. It might be your own root domain, or a hostname given to you by your host or platform.
  2. Open the DNS zone at the service that is authoritative for your domain. If you are unsure which that is, check the nameservers first.
  3. Make sure no other record already exists for www. If there is an old A record for www, remove or replace it, because the two cannot coexist.
  4. Add a record with type CNAME, name www, and value equal to the target. Some panels want a trailing dot for fully qualified names, others add it for you. Follow the panel's hint.
  5. Choose a TTL. A value such as 3600 is typical; use a shorter value while you are testing.
  6. Save and wait for the old answer to expire from caches.
  7. Test with dig www.example.com CNAME +short and dig www.example.com A +short. The first should show the target, the second should show the final address.
  8. Open https://www.example.com in a browser and check that the page loads without a certificate warning. The certificate must include the www name.

A worked example

You run a small shop at mybakery.example hosted on a platform that gave you the hostname bakery123.platform-example.net and an IPv4 address.

You create these records:

Type Name Value
A @ the platform's IPv4 address
CNAME www bakery123.platform-example.net
MX @ the mail provider's server, with priority

Visitors who type mybakery.example go directly through the A record. Visitors who type www.mybakery.example follow the CNAME to the platform's hostname and arrive at the same server. Email to hello@mybakery.example uses the MX record on the root, which works precisely because the root is not a CNAME.

Later, the platform changes its server addresses. Because www is an alias to the platform's own name, it follows the change automatically. The root A record, which you typed yourself, needs to be updated if the platform tells you the address has changed.

Testing and troubleshooting

  • dig www.example.com shows the whole chain, including the CNAME line and the final addresses.
  • dig www.example.com CNAME +short shows only the alias target.
  • If the answer is empty, check for typos in the name, such as ww instead of www, and check that the zone you edited is the authoritative one.
  • If the CNAME resolves but the page shows an error, the problem is probably the web server, which may not be configured to accept the www hostname or to serve a certificate for it.
  • If some visitors see the old site, wait for the old TTL to expire, and compare the answers from different resolvers.
  • If the platform says "CNAME not detected", confirm the exact target value they asked for and that no other record exists on the same name.

Common mistakes

  • CNAME on the root. Many panels reject it; some accept it and then cause email or other records to disappear. Use an A record or the provider's alias feature instead.
  • CNAME alongside other records. Adding a CNAME to a name that has TXT or MX records is invalid and may break email.
  • Wrong target. Pointing www to an IP address in a CNAME is invalid. The value must be a hostname.
  • Missing certificate coverage. If the certificate does not list www, visitors see a warning even though DNS is perfect.
  • Leaving an old A record. An A record and a CNAME on the same name conflict, and behaviour depends on the software.
  • Loops. Two names that point at each other make the lookup fail.

CNAME or A record for www: how to choose

Both approaches are valid, and the right one depends on how your server is addressed.

  • Choose a CNAME when your host or platform gives you a hostname and says the underlying addresses may change. The provider can move your site without asking you to edit anything.
  • Choose an A record, with an AAAA record if you use IPv6, when you have a fixed address that you control, such as a dedicated server. It avoids one extra lookup and keeps the mapping completely under your own management.
  • Choose a CNAME to the root domain when you simply want www to follow the bare domain, and you are happy to maintain addresses in one place.

Whatever you choose, document it. A short note listing each record, its purpose and the date it was changed will save you time months later when you have forgotten why a particular name was set up in a particular way.

Other subdomains that suit a CNAME

The same idea works beyond www. Many services ask you to create a subdomain that points to their hostname, such as shop, help, status or blog. In each case the service gives you a target and you add a CNAME on the subdomain. A few practical habits apply.

  • Use a separate subdomain for each external service, so you can change or remove one without touching the others.
  • Copy the target exactly as given, including any unique identifier inside the hostname.
  • After the service is connected, check that its certificate covers the subdomain before you announce the address to customers.
  • When you stop using a service, delete its CNAME, so the name does not keep pointing at something you no longer control.

Limits and what to watch for

A CNAME adds one more lookup, so a very long chain can slightly slow the first resolution, although caching hides most of this. More importantly, it hands control of the final address to the owner of the target name. If you point www at a platform's hostname, you are trusting that provider to keep that name correct, and if you delete your account there, the name may stop resolving or, in some cases, be claimed by someone else. Remove CNAMEs that point to services you no longer use.

CNAMEs also cannot be used where the standard requires other data. MX and NS records should point to hostnames that have address records, not to names that are themselves aliases. Check the rules of whatever service you are configuring, and when in doubt use the target the provider names exactly.

Finally, use DNS only for domains and servers you are entitled to manage, and follow the terms of your hosting and domain providers.

Summary

A CNAME record makes one name an alias for another. It is ideal for www, because www can follow the root domain or a platform hostname without you maintaining separate addresses. It cannot sit on the root domain and it cannot share a name with other records, which is why the root uses A and AAAA records and why email records live on a name without a CNAME. Create it carefully, avoid loops and stray duplicates, make sure your certificate covers www, and test with dig. If you are choosing or managing a domain to use these records on, the domains page is a sensible next step.

Frequently asked questions

What is a CNAME record?

A CNAME, or canonical name, record makes one hostname an alias of another. When a resolver looks up the alias, it is told to look up the target name instead and use that answer.

Can I put a CNAME on the root domain?

Not in standard DNS. The root of a zone must carry records such as SOA and NS, and a name that has a CNAME cannot have any other record type. Use an A or AAAA record for the root, or a provider feature that offers alias-style flattening.

Should www use an A record or a CNAME?

Either can work. A CNAME is convenient when the target is a hostname that your hosting platform may re-address in future. An A record is fine when you have a fixed IP address and prefer a direct mapping.

Can a CNAME coexist with an MX record on the same name?

No. A name with a CNAME must not have other records, so email for that exact name would break. Keep MX records on a name that has no CNAME, such as the root domain.

Why does my CNAME seem not to work yet?

Common reasons are caching of the old answer, a typo in the target, a CNAME placed on a name that already has other records, or a target that does not resolve. Query the name directly to see what the authoritative server returns.

About the author

Bob Kanokphon Thongdee, founder of XPASS VPN

Bob Kanokphon Thongdee

  • Founder of XPASS VPN
  • Co-founder of eSIMonline.asia
  • More than 7 years in online marketing
  • Founder of Cojourney Visa and Cojourney Travel

Ready to register your domain?

Search a name, compare extensions and pay once for the years you choose.

.com registration from $18.81 for the first year

Search domains

Keep reading

Domain & DNS Guides

What Is an AAAA Record and How Does It Relate to IPv6?

The AAAA record is the IPv6 counterpart of the A record. This guide explains the format, when you need it, how to add it safely and how to test that IPv6 works.

10 min read

Domain & DNS Guides

What Is an MX Record and Why Does Business Email Need One?

An MX record points incoming email for your domain to the right mail servers. Here is how it works, how to set it up step by step, and what can go wrong.

10 min read

Domain & DNS Guides

What Is an A Record and How Do You Point a Domain to a Server?

An A record links a domain name to an IPv4 address. This guide explains how it works, how to point your domain at a server, and what to check when it does not resolve.

10 min read