Multiple Domains for Your Site in Apache

This’ll be a small guide for those of you managing multiple domain names for your site or blog. Say, for example, your domain name is, grandmashirelyshow.com, most likely you’ll also want thegrandmashirelyshow.com, and you might even own the .net and .org version too. Now you have all these domains, but you just have a single blog that you want them to point to. What to do?

Since this is a technical blog, I’m going to assume everyone reading has access to their own server and, as the title says, is running apache. You’ll want to make sure your extra domains are using 301 Redirects rather than just duplicating your site or doing any other type of redirect. The reason for doing a 301 redirect is so that search engines understand what the main domain is, and properly indexes your site. If you have all those domains acting like their own sites, you’ll start competing with yourself, and that isn’t good.

The first thing you need to do is point all those domains to the same server. If you use a common domain registrar, like GoDaddy, you can do this relatively easily. Just get the static IP address of your server and add it to the A name record. Save those changes and you’re all done on the registrar part.

Now ssh into your web server and edit your apache config to setup each of the sites. In my example, I’m using Apache on CentOS, but the steps are the same for most other environments.

Step 1: Create vhosts (.conf files) for each domain

In centOS (following the Debian structure), we put our vhosts within the sites-available folder. If you’re using another system, you may actually have a vhosts.conf file, which you can use as well.

Here is an example of a setup for my two domains.

<VirtualHost *:80>
 ServerName www.grandmashirelyshow.com
 ServerAlias grandmashirelyshow.com

 DocumentRoot /var/www/html/grandmashirelyshow.com
 ErrorLog /var/log/httpd/grandmashirelyshow.com_error_log
 CustomLog /var/log/httpd/grandmashirelyshow.com_access_log combined

 <Directory "/var/www/html/grandmashirelyshow.com">
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
 </Directory>
</VirtualHost>

Add this file to /etc/httpd/sites-available/ directory. You can name it grandmashirelyshow.com.conf.

You’ll want to create a second file in the same directory and name it thegrandmashirelyshow.com.conf and paste the following into it:

<VirtualHost *:80>
 ServerName www.thegrandmashirelyshow.com
 ServerAlias thegrandmashirelyshow.com

 DocumentRoot /var/www/html/thegrandmashirelyshow.com
 ErrorLog /var/log/httpd/thegrandmashirelyshow.com_error_log
 CustomLog /var/log/httpd/thegrandmashirelyshow.com_access_log combined

 <Directory "/var/www/html/thegrandmashirelyshow.com">
  Options Indexes FollowSymLinks
  AllowOverride All
  Require all granted
 </Directory>
</VirtualHost>

Now keep in mind you can have any directory structure you want, along with any error log structure. Many people go with the /var/www/html/sitename.com/public_html structure, that works fine too.

Once you have this part set up, you’ll want to make sure the document root directories you specified actually exist and have content.

Step 2: Include your vhost files in your httpd config

You’ll want to find your httpd.conf file. For CentOS it’s located in /etc/httpd/conf/httpd.conf but in your configuration it may be somewhere else. Once you’ve located it, you’ll need to add a reference to the vhost configs we created above. At the end of httpd.conf add the following line:

IncludeOptional sites-enabled/*.conf

Step 3: Enable your sites

To enable your sites, you just create a symlink from the sites-available conf files to the sites-available directory (you may need to create it).

sudo ln -s /etc/httpd/sites-available/thegrandmashirelyshow.com.conf /etc/httpd/sites-enabled/thegrandmashirelyshow.com.conf
sudo ln -s /etc/httpd/sites-available/grandmashirelyshow.com.conf /etc/httpd/sites-enabled/grandmashirelyshow.com.conf

Step 4: Setup your redirect

Let’s pretend in our example that grandmashirelyshow.com is our primary domain. You’ll want to make sure that its root directory has all your content on it. What we’ll do with the other domain, thegrandmashirelyshow.com, is set that up to have the redirect.

Go to the root directory of your extra domain. Now create a index.html file in there (you can leave it blank). Also create a .htaccess file in there and put the following information in it:

<IfModule mod_rewrite.c>
 Options +FollowSymlinks
 RewriteEngine on
 #Doesn't handle HTTPS requests
 RewriteCond %{SERVER_PORT} ^80$
 RewriteCond %{HTTP_HOST} !^www\.grandmashirelyshow\.com$
 RewriteRule ^ http://grandmashirelyshow.com%{REQUEST_URI} [L,R=301]
</IfModule>

This will make it so any traffic that goes to www.thegrandmashirelyshow.com gets redirected via a 301 to grandmashirelyshow.com. Google and other search engines will understand to index the latter site and you won’t have any competition from yourself!

I hope this little guide helps. I’ve made a ton of assumptions about you, so if you’re having trouble following along, you may want to read up on how to setup and work with a proper Apache environment.

Don't miss another valuable tip

Enjoyed this post? Don't miss out next time I post. Simply signup and you'll get one email each time there's new content that you'll enjoy. No strings attached, just something I do that I hope will help you.

Leave a Reply