How to route/redirect *.foo.org → *.foo.biz via DNS records?

99封情书 提交于 2019-12-24 08:56:11

问题


I registered my domain foo.org and along with it they gave me foo.biz for free. I don't really like it, but as I said, they gave it to me for free... And I want to route absolutely every request involving foo.biz to foo.com, including subdomains.

In the DNS configuration of foo.org, I set up @ and www, as well as the wildcard subdomain * as A records to my host's IP address,

For the DNS configuration of foo.biz, I was thinking of using 301 redirects but they don't play well with wildcard subdomains, or at least I have no idea how to achieve the automatic mapping I want, i.e., without having to do it manually for each subdomain and not use without the wildcard.

Since I really want to make foo.org the canonical name and have foo.biz just as a cheap alias, it makes sense to resort to CNAME records, but how can I do that in order to achieve the mapping???

I'd appreciate your most educated advice!


回答1:


You can't do this by simply using DNS. Sure you can setup a CNAME record for *.foo.biz to foo.org but that won't make browsers request foo.org when a user types foo.biz in her browser's address bar. You have to use redirects on the HTTP server level to achieve this.

Why?

To make myself clear, let's have a look at the DNS setup of stackoverflow.com:

~# dig www.stackoverflow.com
;; QUESTION SECTION:
;www.stackoverflow.com.         IN     A

;; ANSWER SECTION:
www.stackoverflow.com.   300    IN     CNAME   stackoverflow.com
stackoverflow.com.       24     IN     A       192.252.206.16

As we can see, stackoverflow.com is the canonical name of www.stackoverflow.com. But still, if I type www.stackoverflow.com in my browser's address bar, it will send the following request to 192.252.206.16:

GET / HTTP/1.1
Host: www.stackoverflow.com
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

The interesting bit here is that Chrome may have looked at StackOverflow's DNS configuration and may have seen that stackoverflow.com is that server's canonical name, but it still sent www.stackoverflow.com in the Host header field. This allows administrators to host multiple sites on one physical machine (name-based virtual hosting).

However, the server at 192.252.206.16 responds to my request with

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=UTF-8
Location: http://stackoverflow.com/
Date: Mon, 16 Sep 2013 11:00:43 GMT
Content-Length: 148

<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://stackoverflow.com/">here</a></body>

and thus redirects me to stackoverflow.com.

HTTP Server Configuration

In order to achieve your mapping, you need to add some rewrite rules in your HTTP server configuration. For Apache this could look like this:

<VirtualHost *:80>
    ServerName foo.biz
    ServerAlias *.foo.biz

    RewriteEngine On

    RewriteCond %{HTTP_HOST} =foo.biz
    RewriteRule ^(.*)$ http://foo.org$1 [L,R=301]

    RewriteCond %{HTTP_HOST} ^(.*).foo.biz$
    RewriteRule ^(.*)$ http://%1.foo.org$1 [L,R=301]
</VirtualHost>

This will rewrite foo.biz/bar to foo.org/bar and anything.foo.biz/something to anything.foo.org/something. You still need the DNS entries for foo.biz and the site configurations for the foo.org sites, though.

Hope that helps...




回答2:


Adding an A record to redirect site X to site Y should do the trick if all you want is to redirect from foo.biz to foo.org. If what you want, however, is to redirect all aliases of foo.biz to foo.org, then it may be or may not be possible depending on your registrar. You could try to do that by adding a wildcard to a CNAME record. If your registrar does not allow it, you may have to add a CNAME record for each alias. For instance, since a lot of people use the prefix www it is very common to add a CNAME record to it. Depending on your registrar, it should be something like this:

  Type          Name                 Value

   A           foo.biz       (points to) 123.456.789.0

 CNAME          www          (is an alias of) foo.org

 CNAME          etc          (is an alias of) foo.org

If the registrar accepts wildcards on CNAME records:

  Type          Name                 Value

   A           foo.biz       (points to) 123.456.789.0

 CNAME            *          (is an alias of) foo.org


来源:https://stackoverflow.com/questions/18725669/how-to-route-redirect-foo-org-%e2%86%92-foo-biz-via-dns-records

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