Articles: Apache Configuration for Domain Redirection for Unforeseen Sub-domains

Posted by: david on 05/16/2011 - 05:02 PM
One of our websites all of a sudden had visitors through Google via it's name server subdomain of NS1. For instance, people come coming in through (for instance) ns1.Coolcomputing.com instead of www.coolcomputing.com for some reason. Of course, we don't want any sort of duplicate content/listing on search engine results, so what we need to do is to redirect those sub-domain names to the proper one (www). Here's how we did it via Apache to do a permanent 301 redirection (you need tje Apache rewrite module turned on)- example httpd.conf virtualhost configuration chunk below:


ServerName ns1.coolcomputing.com

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} !^coolcomputing.com

RewriteRule ^(.*)$ http://www.coolcomputing.com/$1 [R=301,L]



What I was pleasantly surprised with was that no DocumentRoot or logging directives or needed in a VirtualHost section.


Now I wish there's a cleaner way to do it, for instance a "catch-all" directive to 301 redirect all non "www." to www.coolcomputing.com.