Application Server

Apache

Debian, Apache, SSL, Multiple Virtual Hosts

After much trial and grief, I've finally nailed how to have multiple SSL virtual hosts running on Apache on our Debian servers alongside non-SSL hosts.

In the 'default' apache config, put:


NameVirtualHost *:80
NameVirtualHost *:443

Then for each virtual host config that you ever create, the opening VirtualHost tag MUST include the relevant port as follows:


(for SSL site)
(for standard site)

Note that I have added a space between the tags due to Drupal attempting to format as HTML. Remove the spaces in your config file.

You must then have the ServerName specified inside each Virtual Host.

Somehow it didn't seem that easy at the start!

Rewriting URLs to use www with permanent redirect

The following rewrite rule enables all requests to a site to go through via its www URL, theoretically improving search engine 'optimization' (if there is such a thing).

The important thing here is that any file or directory request following the url is kept during the redirect (I've seen poor rewrites on sites that just redirect from http://whatever.com to the front page and the user loses their path to the link they clicked on once already).


RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=permanent,L]