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

7,384

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>
Share:
7,384

Related videos on Youtube

wflynny
Author by

wflynny

Updated on September 18, 2022

Comments

  • wflynny
    wflynny over 1 year

    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.

    • BE77Y
      BE77Y about 9 years
      What do you get in your access logs? (which access logs are showing the expected activity?)
  • wflynny
    wflynny about 9 years
    I'm sorry, the missing ServerName directives were an error in my example config. I have updated the example given. I have also updated the prompt regarding the DNS.
  • wflynny
    wflynny about 9 years
    It turns out that I was missing some necessary directives in the second *:443 VirtualHost. Adding them fixed my problem. However, because you essentially confirmed that the overall structure of the vhosts.config file was correct, I will accept your answer so anyone searching this in the future can see how to set it up properly.