Virtual Host Redirects


While talking with a friend & colleague today, how I do redirects from non-canonical domain names to their canonical counterpart. So, I figured I'd publish it here for him, and others, to see and perhaps benefit from.

If you haven't read my To www. or not to www. post, go do so to understand why you would want to do this. Go ahead, I'll wait for you.

....

Ok, now that you are back and you understand why you would want to do this, here's how I do it. - To start with we will be creating a new Virtual Host configuration in Apache. (The same thing can be done in IIS.) Why the new Virtual Host? After all, you are correct, we can use RewriteRule directives to conditionally return a redirect based on the Host name under the existing Virtual Host. But if you stop and step back a moment and think about it, Apache (IIS) is already checking what the host name is, to determine which Virtual Host should be used. So, with that in mind, why would we want to tell Apache to check the host name after it just got done doing that very thing? Instead, let's use Apache to our advantage. Thus we create a new Virtual Host configuration that has all of our non-canonical domain names associated with it. With this separate Virtual Host, we can safely return a 301 or 302 HTTP redirect to ANY request that comes in to the non-canonical domain name over to the canonical domain name. (Let's work smarter, not harder.)

Consider the following example:

<VirtualHost A.B.C.D:80>
	ServerAdmin	webmaster@domain.tld
	DocumentRoot	/path/to/DocumentRoot
	ServerName	www.domain.tld
	ErrorLog	logs/www.domain.tld/error_log
	TransferLog	logs/www.domain.tld/access_log
</VirtualHost>
				
<VirtualHost A.B.C.D:80>
	ServerAdmin	webmaster@domain.tld
	DocumentRoot	/path/to/DocumentRoot
	ServerName	domain.tld
	ServerAlias	www.www.domain.tld
	ErrorLog	logs/www.domain.tld/error_log
	TransferLog	logs/www.domain.tld/access_log
	RewriteEngine	on
	RewriteRule	^/(.*)	http://www.domain.tld/$1	[R=301]
</VirtualHost>

Notice how the second Virtual Host directive is identical in everything except the Server Name / Alias(es) and the Rewrite directives. The key difference being that the first Virtual Host is for our canonical domain name and the second Virtual Host is for all other non-canonical domain names. This simple configuration is how we take advantage of the fact that Apache (IIS) is already identifying the host name. We no longer have to re-do the same thing that Apache (IIS) just got done doing for us.

An added advantage of VirtualHost based redirects is that it works for all pages on a site, not just one. Comparatively, a page based redirect only works for the given page, i.e. index.html. If the end user tries to access a different page, they will receive a 404 / Document Not Found message, which can be ambiguous at best and misleading at worst. There is no way for users to be able to tell if the page they have requested is invalid because there is no redirect to the canonical domain name or if the page truly doesn't exist at all.

See Hosting.com's Common rewrite rules for .htaccess article for examples of rules that are using one (or more) RewriteCond(ition) rules to determine if the domain in in use is the canonical domain or not. This is exactly what using additional Virtual Host(s) is meant to address.

See also:
Virtual Host Redirects post.