How to link multiple domains to a website

By , April 24, 2010

Nick from the U.K asks:

My company recently purchased a couple domains and we want those to link to our current website. Eg. example.com will transfer to example.co.uk instantly. How can I do this?

Many Thanks,
Nick

This is similar to an earlier post How do I point “www.domain.com” at “domain.com” or vice-versa?, but since we’re going across multiple domains, we should also think about a couple additional issues.

First, you need to decide if you want all of your domains to hit the website and preserve the domain used, i.e.

http://www.example.com hits the website and keeps that in the location bar, or using http://www.example.co.uk also hits the same webste and also preserves itself in the location bar whenever used.

Or, do you want to pick one “primary” domain, and whenever one of the others are used, you redirect to the primary domain. I.e. You decide to brand under example.com, so when people use example.co.uk to navigate to your website, the location bar goes back to example.com.

If the content across the multiple domains is the same, then you are better off going with the latter. Pick one domain, have all the additional domains resolve back to that main domain. This avoids any “duplicate content” penalties from search engines, and if done correctly (keep reading), would aggregate any page relevance from all domains back to your main domain.

Step 1: Setup the DNS for the additional domains

Setup the records for the additional domains to point at the same IP address as your “primary” domain. In most cases, you can’t use a “CNAME” for the base domain, you need to use a host or “A” record (long story, perhaps the subject of another post). But you can use a CNAME for any subdomains.

 example.co.uk.                          IN A    204.232.193.225
www.example.co.uk.                      IN CNAME www.example.com.

Step 2: Setup the webserver to recognize the additional domains

Then on your web host, you have to make sure your webserver is setup to recognize any additional domains. If your website is the only one on your server, you may not have to do anything.

But in most cases, your website is a “virtual host”, which means the webserver is keying on an http header called “HTTP_HOST” to figure out which website to server up in response to a request, and it needs to map any additional domains to the proper HTTP_HOST.

In apache, this is done using the “ServerAlias” directive in your main domains “VirtualHost” config.


ServerAlias *.example.co.uk example.co.uk

Step 3: Map everything to the primary domain

Then if you’ve decided to map everything to the primary domain, you need to setup some redirects. A piece of PHP code like this:

<?PHP
if($_SERVER["HTTP_HOST"]!="www.example.com") {
$redirect = sprintf("http://www.example.com%s", $_SERVER["REQUEST_URI"]);
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: $redirect");
Header("Connection: close");
exit;
}
?>

Whatever your situation, PHP or not, apache or not, the basic framework is the same:

  • In this case, we’ve chosen “www.example.com” as our primary domain and host. Requests to http://example.com will also be redirected.
  • We also preserve $REQUEST_URI so any deep links into our site from the alternate domains will still hit the right page after the redirect
  • Finally, we use a “301 Redirect” so that any page relevance under the alternate domains will over time be conferred to the URI under our main primary domain.

And that’s all there is to it. I use this exact method often, most recently to map domainHelp.ca to our primary domain domainHelp.com as I wrote the post. You can see it in action with the redirect and deep link here

Leave a Reply