How to redirect domain A to domain B using A-Records and CNAME records only

Solution 1:

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.

Solution 2:

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.


Solution 3:

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]