Virtual Hosts without the www subdomain

An example configuration file for Apache to run a Virtual Host without the www subdomain in the URL

  • 29 Jan 2016
  • Matthew Wittering

When trying to setup a new server recently, I decided that it would be better in the URL didn't include the www subdomain. I've come to think that actually it isn't anything particularly relevant. I actually think it's quite old fashioned, and with focus on clean URLs removing www is a sensible step.

Anyway, after much googling I found you could redirect all the traffic over to the www subdomain to the preferred URL using a the following configurations in the Virtual Host conf file. This was important as I didn't to write this into my Django projects. I believed this was the wrong place to make such a configuration decision. Equally, if I wanted to reintroduce www into the URL string, the configuration change should be made in the conf, instead of the application.

Example

<VirtualHost *:80>
  ServerName www.matthewwittering.com
  ServerAlias www.matthewwittering.com
  Redirect / http://matthewwittering.com/
</VirtualHost>

<VirtualHost *:80>
  ServerName matthewwittering.com
  ServerAlias matthewwittering.com
  WSGIScriptAlias / /path/to/directory/for/wsgi.py
  Alias /static/ /path/to/directory/for/static/
  <Directory />
    Require all granted
  </Directory>
  <Location "/static/">
    Options -Indexes
  </Location>
</VirtualHost>