Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I realise there are lots of different variations on this question on serverfault but I haven't had any luck with combining answers to match our situation.

We have a CentOS 6.2 web server running Apache 2.2 hosting dozens of websites with the same parent domain - i.e.,,

We'd like to add https to some (but not all) of these sub-domains. The sub-domains that are https would run https permanently. For example would redirect to

We have purchased a Comodo Multi-Domain SSL Certificate License for the SSL, which allows us to create certificates for multiple domains. The web server only has a single public facing IP.

How do we setup Apache to do this? If the Virtual Host entry in httpd.conf for is:

<VirtualHost *:80>
  DocumentRoot /var/www/sites/bat
  ErrorLog logs/bat-error_log
  CustomLog  logs/bat-access_log common

What changes should be made to enable SSL on and make sure is still valid, but redirects to

share|improve this question
up vote 1 down vote accepted

You will first need to set up an SSL VirtualHost, so something like:

<VirtualHost *:443>
  DocumentRoot /var/www/sites/bat
  ErrorLog logs/bat-error_log
  CustomLog  logs/bat-access_log common

  SSLEngine on
  SSLCertificateFile /path/to/cert
  SSLCertificateKeyFile /path/to/key

In your non-SSL virtual host, you will need some Rewrite directives to force requests to the corresponding SSL virtual host. So, additional directives to what you have, along the lines of:

   RewriteEngine on
   RewriteRule   ^/(.*)$1 [R,L]

So, all requests to the plain HTTP virtual host will be rewritten to the SSL virtual host.

share|improve this answer
Thank you @cjc - I'm guessing the later code is just added to .htaccess? – BaronGrivet May 15 '12 at 2:34
@Tama The latter is added to the Virtual Host definition for that you included in the question. They will also work in a .htaccess context. Look at the official docs: – cjc May 15 '12 at 2:36
Excellent, thank you. I think the main difficulty I was having is that I'd put far too much stuff in my 443 VirtualHost declaration, having basically gone through the example and included everything. Your (far simpler) example is working great. – BaronGrivet May 15 '12 at 2:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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