问题原文
原帖链接 · user2258740 · CC BY-SA 4.0
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
CNAMEa naked domain to a subdomain. Is that correct?If that's true - and I still want to use
wwwas the default - should I just leave the A entry as is and 301 the naked domain to thewwwdomain? Or is there a better way to do this?
已采纳解答
原帖链接 · DocRoot · CC BY-SA 4.0
I've been apprised that the DNS entries above won't work because you can't
CNAMEa 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.)
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
