问题原文
原帖链接 · 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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
