0

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>

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

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/
</VirtualHost>

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?

Edit:

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.

2 Answers 2

0

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

3
  • 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, 2014 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, 2014 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, 2014 at 13:51
0

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:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .