I have a couple virtual host files. The first one redirects all traffic for http://www.mysite.com to the https version:

<VirtualHost *:80>
   ServerName www.mysite.com
   Redirect permanent / https://www.mysite.com/

<VirtualHost *:443>
   ServerName www.mysite.com
   # additional configuration

The second defines a VirtualDocumentRoot so that sites in directories such as /var/www/www.othersite.com will be served.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    VirtualDocumentRoot /var/www/%0
    VirtualScriptAlias  /var/www/%0/cgi-bin/

I also have NameVirtualHost definitions in the ports.conf file (in case that is helpful):

NameVirtualHost *:443
NameVirtualHost *:80

The problem is that all traffic seems to be redirected to https://www.mysite.com. I would expect that the ServerName www.mysite.com line in the first virtual host file would only match www.mysite.com so that visiting http://www.othersite.com would serve content from /var/www/www.othersite.com. When I comment out the entire port 80 VirtualHost definition from the first file, http://www.othersite.com loads as expected. Am I missing something here? How do I only redirect http to https for www.mysite.com?


The reason I can't put the ServerName as http://www.othersite.com in the second VirtualHost definition is that there are multiple sites in /var/www that also need to be served.


Have you tried to add ServerName www.othersite.com directive to the second virtual host?

  • Thanks for your comment. This would work great, except that there are multiple sites that need to be served via the second VirtualHost file. I edited my question to reflect this. – Divey May 30 '14 at 5:06
  • Well, if you have multiple sites you will need multiple virtual hosts each one with a server name, isn't it? – user221826 May 30 '14 at 5:41
  • That is actually something that you can solve with VirtualDocumentRoot in one VirtualHost configuration. VirtualDocumentRoot will (if I understand it correctly) lookup the directory on the server based on the domain that is requested by the client. I inherited a server that hosted many domains (with document roots such as /var/www/www.othersite.com and /var/www/mydomain.com), only one of which needed to be protected by https, so when I attempted to add the VirtualHost configuration (the first one from my question) I ran into this problem. – Divey May 30 '14 at 13:51

After some digging, I found this answer, which led me to add the following to my second VirtualHost definition:

ServerName host
ServerAlias *

A couple other answers that were very helpful:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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