Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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. cat.example.com, bat.example.com, rat.example.com

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 http://bat.example.com would redirect to https://bat.example.com

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 bat.example.com is:

<VirtualHost *:80>
  ServerName bat.example.com
  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 bat.example.com and make sure http://bat.example.com is still valid, but redirects to https://bat.example.com?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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

<VirtualHost *:443>
  ServerName bat.example.com
  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   ^/(.*)         https://bat.example.com/$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? –  Tama May 15 '12 at 2:34
@Tama The latter is added to the Virtual Host definition for bat.example.com that you included in the question. They will also work in a .htaccess context. Look at the official docs: httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule –  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. –  Tama May 15 '12 at 2:41
add comment

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.