With Dreamhost as my web host, I had gotten in the habit of using subdomain redirects to create convenient URL’s, both for users and myself. So to get to the Scrawlers blog one could enter “blog.scrawlers.com” in one’s web browser. For my own convenience, I’d typically have a subdomain to redirect to the blog’s administration panels. And so on.
Now that I’m with a real VPS, Slicehost (referral link), I need to get more hands-on in managing these subdomain redirects. In a word, I need to start using Apache virtual hosts.
But first, I needed to setup a DNS record for the subdomain. This assumes you have your other DNS stuff for your domain working. Navigate through your Slice’s DNS record menus to create a new record for the target domain.
- Under the Slices tab, click “DNS Records”
- For the target domain, click “records”
- Click “new record”
In this example, I’m going to forward goog.scrawlers.com to the main Google search page. So enter “goog” as the Name and your Slice’s IP address as the IP Address. Clearly my IP Address is made up.
Now to update Apache to handle redirecting. I used the excellent deprec tutorial to install my Rails stack, so Apache resides in /usr/local/apache2 on my slice. For redirection, I chose to make a new directory in the configuration area, and create a virtual hosts file per domain. My virtual host file, in this case, is located at /usr/local/apache2/conf/redirects/scrawlers.com.conf. This definition is pretty simplistic
<VirtualHost *:80> ServerName goog.scrawlers.com Redirect permanent / http://www.google.com </VirtualHost>
In reality I have about 8 VirtualHost definitions in my scrawlers.com.conf file. If you had defined multiple subdomains to redirect to the same place, say to account for typos or pluralization, there is no need to define multiple VirtualHosts. Simply use the ServerAlias keyword. For example:
<VirtualHost *:80> ServerName goog.scrawlers.com ServerAlias googs.scrawlers.com ServerAlias googoo.scrawlers.com Redirect permanent / http://www.google.com </VirtualHost>
Finally, you need to edit your base Apache config to include all configurations defined in the “redirects” directory. In my case, this base config file is at /usr/local/apache2/conf/httpd.conf. At the end of this file, add the line:
Now restart Apache, either through capistrano (cap apache_restart) or directly on the server. Depending on how quickly you work, you may need to give DNS some time to sort itself out. It shouldn’t take more than an hour, though.