Search code examples
phpwebserverapache2virtualhost

Rewriting Virtual hosts to Automatically redirect to SSL (Certbot Certificates)


Currently i have a web server that has two subdirectories that point to a.example.com and b.example.com. I ran certbot and managed to get an ssl certificate running for them both fine. if i type https instead of http into the web address it will load the page secure.

The problem is that i want a redirect to https WITHOUT mod_rewrite. I understand it's bad practice.

current virtual host:

<VirtualHost *:80>
        ServerName a.example.com
        ServerAlias www.a.example.com
        DocumentRoot "/var/www/html/a/"
</VirtualHost>
<VirtualHost *:80>
        ServerName b.example.com
        ServerAlias www.b.example.com
        DocumentRoot "/var/www/html/b"
</VirtualHost>

I'm fairly new to virtual hosts, however. I have two ssl files in my sites-available directory. One is Default-ssl.conf and the other is 000-default-le-ssl.conf

Which of these is one i should add 443 virtual hosts to? I did some reading and from what i understand i need to change my port 80 hosts in default to redirect to the https. If that's so, do i need to declare my document root and ANOTHER alias in there as well?

Edit: It doesn't actually redirect the https://www.subdomain. It just brings back server not found.


Solution

  • For anyone who's struggling with this and finds this question, don't forget to tell apache the sites are available.

    sudo a2ensite example.com.conf