Ssl – Two https virtualhosts on same server: same port, different subdomains

apache-2.2sslvirtualhost

I've tried searching for this scenario, but can't seem to find exactly this setup. I want to have something along the lines of the following:

I want to have two subdomains redirect to different https sites with different document roots. The ssl cert we have is domain level (*example.com). The following doesn't seem to work, in that when I put more than one virtualhost for each port, I get "This webpage is not available". Any suggestions?

NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
  ServerName subdomain1.example.com
  RedirectPermanent / https://subdomain1.example.com
</VirtualHost>
<VirtualHost *:80>
  ServerName subdomain2.example.com
  RedirectPermanent / https://subdomain2.example.com
</VirtualHost>
<VirtualHost *:443>
  DocumentRoot /srv/www/subd1/
  ServerName subdomain1.example.com
  # more directives
</VirtualHost>
<VirtualHost *:443>
  DocumentRoot /srv/www/subd2/
  ServerName subdomain2.example.com
  # more directives
</VirtualHost>

We have the DNS directing both subdomain1.example.com and subdomain2.example.com to our server IP address.

Edit: Fixed an error in the example config (no ServerName directive) and added info regarding our DNS.

Best Answer

Your config sample should work (assuming that you have properly set the DNS entries corresponding to those virtual hosts to resolve to the server in question), but you don't seem to have included ServerName directives - try the following (assuming that this isn't just an error in construction of your example config):

NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>
  ServerName subdomain1.example.com
  RedirectPermanent / https://subdomain1.example.com
</VirtualHost>

<VirtualHost *:80>
  ServerName subdomain2.example.com
  RedirectPermanent / https://subdomain2.example.com
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot /srv/www/subd1/
  ServerName subdomain1.example.com
  # more directives
</VirtualHost>

<VirtualHost *:443>
  DocumentRoot /srv/www/subd2/
  ServerName subdomain2.example.com
  # more directives
</VirtualHost>
Related Topic