I have the following setup in my http.conf:

<VirtualHost *:80>
  UseCanonicalName    Off
  VirtualDocumentRoot "/var/www/sites/%0/_site"

It all works great (I have /var/www/sites/www.example1.com/_site, /var/www/sites/www.example2.com.au/_site, etc). Adding a site is as simple as adding a directory!

However, I now want to make sure that regadless of what domain was called, if a user goes to example1.com, she will be HTTP redirected to www.example1.com.

Is there an easy way to do this without losing the brevity above? I have about 84 hosts, and am trying to optimise my httpd.conf... Merc.

  • Is it important that it should be a HTTP redirect, or would it work equally well if both the www and the non-www sites were simply to use the same directory? – Jenny D Sep 23 '16 at 13:23
  • It's crucial that it's a redirect... – Merc Sep 24 '16 at 0:27

The simplest would be to simply create the corresponding directories for the bare domain names and redirect from there with individualized .htaccess files. I'm usually quite averse to enabling .htaccess files but this might be a good use case. If you don't already allow them, the following will set up the permission to use them in a limited fashion:

<Directory /var/www/sites/*/_site/> 
   AllowOverride FileInfo

And then in each directory for a site with a bare domain name an .htaccess file with:

Redirect permanent / http://www.example.com

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.