2

Let's say I own example.com and want to use www.example.com as the default.

I thought I could do this by using an A record to point the www version to the IP and using a CNAME record to point the naked domain to the www version.

Like this:

www.example.com A 14400 192.0.2.123
example.com CNAME 14400 www.example.com

Two questions:

  • I've been apprised that the DNS entries above won't work because you can't CNAME a naked domain to a subdomain. Is that correct?

  • If that's true - and I still want to use www as the default - should I just leave the A entry as is and 301 the naked domain to the www domain? Or is there a better way to do this?

  • Does this answer your question? What are the most commonly used and basic Apache htaccess redirects? – Steve Jan 17 at 20:40
  • I appreciate the link, but I don't think it addresses either of my questions. To clarify, I'm not asking HOW to configure this as much as IF I should use this configuration. – user2258740 Jan 17 at 20:57
  • You can not have a CNAME record at apex (example.com in your case) based on how CNAME is defined, it can not coexist with any other record (except DNAME and RRSIG), while the apex already has SOA and NS records even if you do not see them. You typically do the opposite, add an A record on apex, and have www be a CNAME to apex. It is just one way to do things among others. – Patrick Mevzek Jan 17 at 21:13
  • PS: 123.456.789 is not a valid IPv4 address, even from very far. I edited your question to replace that. – Patrick Mevzek Jan 17 at 21:14
  • I appreciate the suggestions--and the configuration you proposed (A for the apex and CNAME for the www) will work. But, can you explain why this is better than the version I proposed (A for the www and 301 the apex to the www)? Not second-guessing. Just trying to understand the pros/cons. – user2258740 Jan 17 at 22:11
2

I've been apprised that the DNS entries above won't work because you can't CNAME a naked domain to a subdomain. Is that correct?

Yes, as stated in comments, you can't create a CNAME record on the domain apex, ie. example.com. Trying to do so can cause problems later.

You would typically do this the other way round. For example:

example.com.  A      14400  192.0.2.123
www           CNAME  14400  example.com.

Alternatively, you could create www as another A record to the IP address. But this is unnecessary if they point to the same place - it would require repeating the IP address.

These DNS records simply direct traffic to example.com or www.example.com to your server. Both are "equal". Neither one or the other is the "default".

To make one or other "default" you need to 3xx redirect one to the other.

to use www as the default - should I just leave the A entry as is [for www] and 301 the naked domain to the www domain?

You would still need to create a DNS A record for the "naked" domain, otherwise it simply won't resolve (it won't reach your server). And then you can implement the 301 redirect.

However, implementing A (domain apex) + CNAME (www subdomain) would generally be preferable to A (domain apex) + A (www subdomain) as it's arguably easier to maintain. Although functionally it would be the same.


Aside: There is another (middle) step not covered here that also needs to be done in order to configure a site for example.com and www.example.com. The webserver itself needs to be instructed to accept requests for both. On a shared host, this will all be done for you. (For instance, you can't simply point a domain at a webserver - with a DNS A record - and expect that webserver to respond as you expect.)

| improve this answer | |
  • 1
    This is very helpful--particularly the aside (which I would not have guessed). Many thanks. – user2258740 Jan 18 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.