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

Should I use CNAME or Apache virtual host for www?

· 52CE 我爱测

At this time I have a CNAME record with host "www" and pointing to example.com . But with this setup, when a user types www.example.com , he gets "redirected" (I know it's not an actual redirect) to example.com . I would like how…

问题原文

原帖链接 · Gianf DS · CC BY-SA 4.0

At this time I have a CNAME record with host "www" and pointing to example.com. But with this setup, when a user types www.example.com, he gets "redirected" (I know it's not an actual redirect) to example.com. I would like however to have the address stay to www.example.com when he types it.

I therefore think to delete the CNAME record and create a virtual host on Apache. What do you think would be the most correct thing I should do?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 4.0

If the URL is changing from example.com to www.example.com for users, then there IS an actual redirect happening. However it has nothing to do you with CNAME record, because CNAMEs can't cause redirects. They simply point a domain or subdomain to the same server as another host name. So your CNAME record is causing both your bare domain and your www subdomain to resolve to the same server, but your web server is what is causing the redirect. To solve this problem, you won't need to touch your DNS records at all. Instead you'll need to modify your web server configuration.

Within Apache is is very common to point both the bare host name and the www subdomain to the same directory and configure the redirect using rewrite rules in .htaccess. If that is the case, you should delete the rewrite rules or change them to make the redirect go the other direction.

It is also possible to configure different virtual hosts for the bare domain and the www subdomain and use redirect directives in one or the other. If that is the case, you will need remove the redirect directive from one of your virtual hosts and add it to the other.

Another possibility is that you have a virtual host for www but some default catch-all virtual host for every other name that is pointing to your server that redirects. If that is the case, you should create a new virtual host for your bare domain, or add a ServerAlias to your virtual host for your www subdomain so that the same virtual host also handles the bare domain.

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

资料来源与适用范围

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

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

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