How do I create a virtual host that works with both http and https?

8

1

This is how I have set up a virtual host:

<VirtualHost mysite> 
  <Directory "/Users/myusername/sitefolder"> 
    Options +FollowSymlinks
    AllowOverride All 
    Order Allow,Deny
    Allow from all
  </Directory> 
  DocumentRoot "/Users/myusername/sitefolder"
  ServerName mysite
  SSLEngine on
  SSLCertificateFile /Users/myusername/certs/server.crt
  SSLCertificateKeyFile /Users/myusername/certs/server.key
</VirtualHost>

With this configuration, I can view my site only with https, but not http. When I turn SSLEngine off then I cannot view my site with https, but http works fine.

How can I adjust the above lines so that I am able to see my site using both http and https?

I using OSX Lion, but I don't think it matters that much.

Thanks.

Baha

Posted 2011-07-31T19:31:26.753

Reputation: 637

Answers

7

You need to create two virtual hosts thus:

<VirtualHost mysite:80> 
  <Directory "/Users/myusername/sitefolder"> 
    Options +FollowSymlinks
    AllowOverride All 
    Order Allow,Deny
    Allow from all
  </Directory> 
  DocumentRoot "/Users/myusername/sitefolder"
  ServerName mysite
</VirtualHost>


<VirtualHost mysite:443> 
  <Directory "/Users/myusername/sitefolder"> 
    Options +FollowSymlinks
    AllowOverride All 
    Order Allow,Deny
    Allow from all
  </Directory> 
  DocumentRoot "/Users/myusername/sitefolder"
  ServerName mysite
  SSLEngine on
  SSLCertificateFile /Users/myusername/certs/server.crt
  SSLCertificateKeyFile /Users/myusername/certs/server.key
</VirtualHost>

The first is a regular HTTP host, while the second handles your HTTPS traffic.

Mike Insch

Posted 2011-07-31T19:31:26.753

Reputation: 2 363

Is there any way to move the common code into a different file and #include it to make maintenance easier? – Ponkadoodle – 2013-12-18T06:11:04.430

2

You also probably want to use Include directive so you don't have to duplicate config between your two vhosts - http://httpd.apache.org/docs/2.2/mod/core.html#include.

spinkus

Posted 2011-07-31T19:31:26.753

Reputation: 160