0

In my default-ssl

 <VirtualHost _default_:443>
  ServerName example.co
  DocumentRoot /var/www/html/example
  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerName example2.co
  DocumentRoot /var/www/html/example2
  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example2.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example2.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example2.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerAlias subdomain1.*
  DocumentRoot /var/www/html/example/subdomain1
 </VirtualHost>
 <VirtualHost *:443>
  ServerAlias subdomain2.*
  DocumentRoot /var/www/html/example/subdomain2
 </VirtualHost>

I would like to apply the SSL certificate to the domain but apply a document root to different subdomains.

For some reason,

https://subdomain1.example.co/test is SECURE
https://subdomain1.example2.co/test is NOT SECURE

I have a wildcard SSL certificate for both.

Both should work.

Why can I designate SSL to all example.co and it's subdomains without a problem? Yet I can't do the same with example2.co?

I am trying to avoid creating lots of code for things are are pretty logical. Is there a shorter way to do this (my only solution):

 <VirtualHost *:443>
  ServerName example.co
  DocumentRoot /var/www/html/example
  SSLCertificateFile /etc/letsencrypt/live/example.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerName example2.co
  DocumentRoot /var/www/html/example2
  SSLCertificateFile /etc/letsencrypt/live/example2.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example2.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example2.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerName subdomain1.example.co
  DocumentRoot /var/www/html/example/subdomain1
  SSLCertificateFile /etc/letsencrypt/live/example.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerName subdomain1.example2.co
  DocumentRoot /var/www/html/example2/subdomain1
  SSLCertificateFile /etc/letsencrypt/live/example2.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example2.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example2.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerName subdomain2.example.co
  DocumentRoot /var/www/html/example/subdomain2
  SSLCertificateFile /etc/letsencrypt/live/example.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerName subdomain2.example2.co
  DocumentRoot /var/www/html/example2/subdomain2
  SSLCertificateFile /etc/letsencrypt/live/example2.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example2.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example2.co/chain.pem
 </VirtualHost> 
1

This should be working with VirtualDocumentRoot:

 <VirtualHost *:443>
  ServerName example.co
  DocumentRoot /var/www/html/example
  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerName example2.co
  DocumentRoot /var/www/html/example2
  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example2.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example2.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example2.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerAlias *.example.co
  VirtualDocumentRoot /var/www/html/example/%-3
  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example.co/chain.pem
 </VirtualHost>
 <VirtualHost *:443>
  ServerAlias *.example2.co
  VirtualDocumentRoot /var/www/html/example2/%-3
  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/example2.co/cert.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/example2.co/privkey.pem
  SSLCertificateChainFile /etc/letsencrypt/live/example2.co/chain.pem
 </VirtualHost>

In this config for every subdomain the document root is set dynamically. The %-3 takes the third part (counted from the right) of the requested domain and adds it to the path. For example sub1.example.co will result in /var/www/html/example/sub1 and abcd.example2.co will result in /var/www/html/example/abcd. If you add a subdomain e.g. sub123.example.co you don't even need to touch this config because the path for this subdomain is set dynamically :)

Additional Info: There are subtle differences between ServerName and ServerAlias:

ServerName can accept port numbers as well, while ServerAlias cannot.

ServerAlias can accept wildcards (eg. *.mywebsite.com), while ServerName cannot.

That's why you should use ServerAlias instead of ServerName for wildcard subdomains. I don't know if ServerAlias is working without ServerName. If not just add a ServerName line with a random subdomain name eg. any.example.co to the two wildcard subdomain vhosts ;)


Link to post where I got initial info about this: https://stackoverflow.com/questions/13566226/creating-wildcard-sub-domain-using-apache-virtualhost

Link to apache2 docs about this: https://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html

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.