大网数据广告活动
网站运营与故障问答

Is a CNAME from the domain apex to the www record the correct way to configure a site to use the WWW version?

· 52CE 我爱测

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. …

问题原文

原帖链接 · 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 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?

已采纳解答

原帖链接 · DocRoot · CC BY-SA 4.0

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.)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2020-01-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。