I recently acquired some domains and finally got around to adding them to my server. Instead of them being their own sites (well, point to my primary site, but the URL in the browser address bar is taken over by the new domain name), I wanted them to redirect to my primary domain (phillipcooper.co.uk).
If you want to achieve the same effect, here is how I did it. All you need is Apache, mod_rewrite, and htaccess. I’m going to skip the step-by-step configuration stuff to cut down the length of this article. If you need help, you can register for an account and post a question to the comments section for this post.
In this mock setup, I’ll use 10.10.100.34 as the IP address of my web server, example.com as the primary domain I want to redirect to, and ourexamples.com as the domain I want to redirect from (to example.com). This is a three step process that involves creating the zone file(s), adding the redirect domains to your Apache httpd.conf VirtualHost configuration, and finally creating the htaccess mod_rewrite rules to redirect all requests for the new domains to your desired domain.
Create a New Zone File for each Domain you Acquired
The primary domain example.com already has a zone file and is a functioning web site, but for ourexamples.com to work we need to create a zone file for it. Here is an example (we only need the minimum – SOA, NS, and the host for web).
$TTL 21600 $ORIGIN ourexamples.com. @ IN SOA ns1.example.com. hostmaster.example.com. ( 2008012601 ; serial 3600 ; refresh 600 ; retry 86400 ; expiry 21600 ) ; minimum IN NS ns1.example.com. IN NS ns2.example.com. IN A 10.10.100.34 www IN A 10.10.100.34
Now reload the zone(s) for changes to take effect.
Add the Redirect Domains to the Primary Domain’s VirtualHost Definition (using the ServerAlias directive)
Edit your primary domain’s virtual host entry and add the new domain(s) using theServerAlias directive. Below is an example of editing the example.com virtual host and adding the new redirection domain ourexamples.com (highlighted line).
<VirtualHost *:80> ServerAdmin hostmaster@example.com ServerName example.com ServerAlias www.example.com ServerAlias ourexamples.com www.ourexamples.com DocumentRoot /web/example/html ScriptAlias /cgi-bin/ /web/example/html/cgi-bin/ ErrorLog /web/example/logs/error_log CustomLog /web/example/logs/access_log combined <Directory "/web/example/html"> AllowOverride All </Directory> </VirtualHost>
Restart Apache to reload the virtual host changes. Next we’ll need to create the htaccess file to configure domain redirection.
Using mod_rewrite to Redirect the Domain Requests
If we were to stop here, the new domain would work, however there is no redirection to your primary domain name. It will simply use the same document root and serve the files without any changes to the URL in the browser address bar. If this is what you desire, then stop here, otherwise continue on to bounce all requests from ourexamples.com to example.com.
In the document root of where your HTML files are stored (where DocumentRoot points to in your Apache VirtualHost definition), create a .htaccess with a text editor. Here is the example.
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule (.*) http://www.example.com/$1 [L,R=301] </IfModule>
In short, what this code does is turn on mod_rewrite’s engine, check the HTTP host to see if it matches www.example.com and also checks for empty references; if either case is a yes it rewrites the URL to http://www.example.com using a permanent redirect (HTTP 301 Redirection header). The $1 appends any portion trailing the domain name from the original (directories and/or pages being accessed). This code also forces the ‘www’ prefix on all requests — http://example.com => http://www.example.com andhttp://ourexamples.com => http://www.example.com and so on.