68

I have 2 domains hosted with different hosts. I need to redirect Domain A to Domain B. Unfortunately I can't do a 301 redirect from Host A, but can only modify/add DNS entries (A-Records and CNAMEs) at Host A.

Surely it is possible to redirect www.DomainA.com to www.DomainB.com using only A-records and CNAMEs?

At present, the DNS entries are:

DomainA.com.    3600    IN    SOA       ns1.HostA.net.
www             3600    IN    CNAME     www.DomainB.com.    
DomainA.com.    3600    IN    NS        ns1.HostA.net.  
DomainA.com.    3600    IN    NS        ns2.HostA.net.  
DomainA.com.    3600    IN    NS        ns3.HostA.net.

I want to redirect

DomainA.com -> DomainB.com
*.DomainA.com -> *.DomainB.com

I've tried the suggestion from this other post but it didn't work.

How can I achieve this only with A-Records and CNAMEs please? Thank you for your advice.

Prembo.

Prembo
  • 867
  • 1
  • 6
  • 11
  • 2
    What kind of redirect are you looking to achieve? DNS is ***not*** capable of acting like an HTTP redirect in any way - it's not possible to change what host a browser is trying to access in DNS alone. – Shane Madden May 03 '12 at 23:35
  • I understand that a 301 redirect is ideal, and that is what we intend to do in the near future (unfortunately domain A is stuck with Host A for another 50 days, and cannot be transferred due to a lockout period. And host A won't let you do HTTP redirect unless you pay for an extra 'package'). So for the time being, we're just trying to redirect any traffic in anyway from domain A to domain, before we move domain A across to Host B. – Prembo May 03 '12 at 23:51
  • 2
    DNS **can not** do redirects. The **only** thing DNS can do is alias (one name looks the same as another, like a copy). That is not the same thing as sending people who arrive at one place to a different place. – Chris S May 05 '12 at 02:28
  • 1
    Thanks for clarifying the difference between a HTTP redirect and DNS resolution/delegation. I understand now. – Prembo May 06 '12 at 09:43

6 Answers6

45

So you are not looking at redirection as such (as that happens at the app level, i.e. on Apache/Nginx/wherever) but rather on the DNS resolution. The host on which DomainA is hosted will or should never be hit, based on your description as you want the DNS requests to be resolved to the IPs of DomainB. Unless I'm missing something in your request?

As Shane pointed out DNS is not capable of HTTP redirection - that's an application/webserver duty. You could make DomainA and DomainB resolve to the same IP on DNS and all would work. But if you're looking to do this on per URL/per-path way then this is not possible - DNS is not capable of that - it's a simple DNS->IP service, what's happening with the actual URL is the webserver's task.

After the comment below, what I'd do is to refer all DNS records for DomainA to the same IP(s) as DomainB is pointed to - this way you will get HTTP request hitting hostB and then it's just a simple matter of:

  1. creating a particular Apache Name Baseed Virtual host - which will be serving files from its own DocumentRoot
  2. creating permanent redirect on Apache like this:

This will rewrite anything coming to DomainB to DomainA which can be hosted on the same server or somewhere else. I appreciate that the second option is probably an overhead and not necessary if you can/are allowed to create Name Based Virtual hosts on apache.

<VirtualHost *:80>
  ServerName DomainB
  Redirect permanent / http://DomainA/
</VirtualHost>

I'd go with 1. - point all DNS records of DomainA to the same IP(s) as DomainB is pointing and create particular Name Based VirtualHosts on Apache.

kasperd
  • 29,894
  • 16
  • 72
  • 122
milosgajdos
  • 1,808
  • 2
  • 21
  • 29
  • Yes you understand correctly - please see my comment above. So what DNS entries would help to achieve that objective? There is not web hosting at Host A, just the domain is held there. And we want all traffic to domain A to resolve to Domain B... Many thanks for your time. – Prembo May 03 '12 at 23:54
  • Is this the same as adding ServerAlias `domain B` to the existing virtual host for `domain A`? – BadHorsie Aug 03 '16 at 14:14
12

As others have stated, it's not possible to perform HTTP redirection with DNS alone. DNS and HTTP work together to redirect a user from one web page to another.

You can use DNS by itself to make domain A show the same content as domain B, but the web browser will show domain A in the URL. You need to be very careful with this as it's quite bad from an SEO perspective. You'll get into trouble for "duplicate content", and search engines will punish you for it.

The correct (from a UX and SEO perspective) way to make this work is to perform an HTTP redirect from Domain A to Domain B.

You can manually configure your web servers (Apache, Nginx, IIS, etc.) to do this work for you if you are technically able. There are lots of good resources to help with that around the web.

If you can't or don't want to control the infrastructure you can use a URL redirection service to perform this job for you. I am the founder of one called EasyRedir, but there are certainly others.

wgrrrr
  • 121
  • 1
  • 3
7

Domain A needs to have the same IP addresses as in domain B. There is no need for any reference to B in A's definition. Web browsers do not "follow" CNAMEs as redirection, they will keep the same hostname and use the CNAME's own record to resolve to an IP address.

If you wish for B to appear in the browser when A is visited (optional and unrelated to DNS), you need to match the HTTP Host and redirect the requests on the Web server. with Apache, you would use mod_rewrite to do it like this in a virtual host definition:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domainB.com$
RewriteRule ^/(.*)$ http://www.domainA.com/$1 [R=301,L]
Kl4m
  • 361
  • 2
  • 6
  • Are there any advantages of this syntax over the accepted answer? More control of the regex and HTTP status-code? – Jeppe May 05 '20 at 10:37
  • While the accepted answer works, I prefer this because of the extra control. This method is slightly more overhead but worth it. Specifically you retain the folder/page info with this redirect. You can even log the activity. In one situation if the user was trying to access the login page, we retained a mirror login page (connected to the same db) and if the user was validated (user/pass match) we would redirect the user to the new domain, inform them of the new domain / move and have them login again (to trigger password managers to store the new credentials and avoid SSO headaches). – srt8driver Apr 06 '21 at 07:10
0

If you really wanted to redirect a domain with only DNS records and are OK with third-party services, you may use forwarddomain.net.

It works by setting your source domain (e.g. www.olddomain.com) CNAME to r.forwarddomain.net (or A+AAAA records of that) and set the target domain URL by writing a TXT record with _ prefix (e.g. _.www.olddomain.com) to that domain with forward-domain= prefix (e.g. forward-domain=https://newdomain.net). The full DNS record will be:

www.olddomain.net.      IN  CNAME  r.forwarddomain.net
_.www.olddomain.net.    IN  TXT    forward-domain=https://newdomain.net/*

That will redirect www.olddomain.net to newdomain.net, also works with HTTPS.

willnode
  • 126
  • 4
0

Of course it is possible to redirect, with the following trick:

  1. Create a new standard primary zone
  2. Name it same as the fictive URL that you want to redirect to
  3. Ensure that this fictive name is different than any AD DNS name
  4. Create A record with following entries:

blank.......................A............................ip-addr-2

www.........................A............................ip-addr-2

What we have here is redirection, essentially. A valid URL will resolve based on the existing DNS primary DNS zone. A fictive URL will be redirected to ip-addr-2. What is important is that the name of this entry is blank, so it will fall down to the next entry in the record and redirect to ip-addr-2

Bran Kop
  • 11
  • 1
-1

I haven't tried it, but I think you can do this:

DomainA.com. CNAME DomainB.com.

This is instead of any other records in the DomainA zone file at all. No SOA, nothing.

Don't try this on a domain that you care about working.

wfaulk
  • 6,828
  • 7
  • 45
  • 75
  • I don't believe that this will redirect the resolution of subdomains, despite your leaving out the SOA. DNAME is designed to do this, but for some reason nobody understands, CNAME and DNAME are mutually exclusive. – Falcon Momot May 05 '12 at 02:50
  • 3
    You cannot place `CNAME` records on naked/apex domains like `domain.com`, but you can on subdomains like `www.domain.com` – Petrus Theron Mar 16 '15 at 05:56
  • This will not **redirect**, it'll only resolve www.DomainA.com to the IP of DomainB; and it only works for sub-domains (not naked domain0> – Wernight Sep 14 '16 at 09:29