DNS 将 domain.com 重定向到 www.domain.com [英] DNS redirect domain.com to www.domain.com

查看:26
本文介绍了DNS 将 domain.com 重定向到 www.domain.com的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对该问题进行了网络搜索,但对结果完全失望.案例是将所有对domain.com的请求重定向到子域www.domain.com.

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.

我所拥有的:

  1. www.domain.com - 主网站域,所有客户端请求都应该是重定向到这里
  2. domain.com - 另一个网站入口点不使用 www 前缀的人,所有请求都应该重定向到 www.domain.com
  3. mydomain.com - 替代网站别名,全部请求应重定向到 www.domain.com
  4. www.mydomain.com - 对于使用 www 前缀的人,所有请求都应重定向到 www.domain.com
  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

我知道使用 .htaccess 和 PHP 可以做到这一点.但我想弄清楚如何仅使用 DNS 来完成此操作.我也明白 DNS 查询的结果不会改变 HTTP 层中发生的事情,因此最初输入的域名将始终是发送到主机中的 Web 服务器的域名.因此,要将 domain.com 重写为 www.domain.com,我仍然需要 Apache mod_rewrite.但我想用 DNS(CNAME 和 A 记录)做主要工作.

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

那么主要的问题是上面的每个域应该有哪些 CNAME 和 A 记录?

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

推荐答案

您可以将 www.domain.com 设为 A 记录,并将 www.domain.com 的所有其他域名设为 CNAME.但这只能解决"如果 www.domain.com 的 IP 地址发生变化,您不必更改其他 DNS 实体,因为它们是别名.

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.

因此在 DNS 级别上无法强制执行重定向.并且有一个很好的理由,因为 DNS 不仅用于 HTTP.例如,如果对 domain.com 的所有请求都重定向到 www.domain.com,您的电子邮件地址将更改为 user@www.domain.com.

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.

因此,对于 HTTP 重定向,您必须使用 HTTP 解决方案.这可以在网络服务器级别(mod_rewrite、代码、javascript(呃)等),但您也可以在您的网络服务器前使用代理来处理此问题.

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.

这篇关于DNS 将 domain.com 重定向到 www.domain.com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆