DNS redirect domain.com to www.domain.com [closed]

僤鯓⒐⒋嵵緔 提交于 2021-01-20 16:13:31

问题


I have performed a web search for the question but totally disappointed with the results. The case is to redirect all requests to domain.com to subdomain www.domain.com.

So what I have:

  1. www.domain.com - main website domain, all client requests should be redirected here
  2. domain.com - another website entry point for people not using www prefix, all requests should be redirected to www.domain.com
  3. mydomain.com - alternative website alias, all requests should be redirected to www.domain.com
  4. www.mydomain.com - for people using www prefix, all requests should be redirected to www.domain.com

I understand this is possible using .htaccess and PHP. But I want to figure out how this can be done using DNS only. I also understand that the result of the DNS query doesn't change what happens in the HTTP layer so the originally entered domain name will always be the one that's sent to the web server in the Host. So to rewrite domain.com to www.domain.com I will still need Apache mod_rewrite. But I want to do main part of work with DNS (CNAME and A records).

So the main question is what CNAME and A records each domain above should have?


回答1:


You could make www.domain.com the A record and all the other domainnames CNAMEs of www.domain.com. But this only "solves" that if the IP address of www.domain.com changes you don't have to alter the other DNS enties as they are aliases.

So on the DNS level there is no way to enforce a redirect. And for a good reason because DNS is used for more then only HTTP. For example if all request for domain.com would redirect to www.domain.com your email addresses will change to user@www.domain.com.

So for HTTP redirection you will have to use an HTTP solution. This can be at the webserver level (mod_rewrite, in code, javascript (ugh), etc..) but you could also have a proxy in front of your webserver to handle this.



来源:https://stackoverflow.com/questions/15172394/dns-redirect-domain-com-to-www-domain-com

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!