Apache Virtual Hosts and Domain Redirection

You want to have the ability to redirect a subdomain but how best to go about it?  One way is to use Apache's Virtual Hosts (especially if you want to avoid using .htaccess), Redirect and the mod_alias module.

I wanted to redirect the entire www subdomain to my root domain so I did the following in my Virtual Host configuration file (located in apache2/sites-available):​

Breaking down the components of the Redirect statement looks like this:​

  • Redirect type: you can use permanent OR 301 as well as any of the other HTTP standard codes for redirect
  • Path to be redirected: I wanted the whole of the website redirected (hence I specified '/' ) but you could just as easily specify a subdirectory
  • URL to be redirect to: If the request had a path that did not exactly match the second argument, then all bits that followed would be appended to the destination URL (e.g. if the request was www.yourdomain.com/taco/bar.html, the redirect would look like yourdomain.com/taco/bar.html

You can still use the Redirect command in .htaccess if you so choose, it's just not my preference.​