Ubuntu – Force https:// and www. with virtual host apache2

Apache2redirectservervirtualhost

I've been looking everywhere for a solution to be able to force https ://www in front of all my URL's using virtual host. This is what I currently have in my 000-default.conf file:

<VirtualHost *:80>
ServerName www.domain.com
Redirect / https://www.domain.com/
</VirtualHost >

That means all www. would redirect to https ://www.

Now I'm missing the code to redirect all/any other forms of URL to https://www. using the same concept (not RewriteEngine because Apache suggests not to use RewriteEngine on simple redirects).

For example:

  • http ://domain.com
  • http ://www.domain.com
  • https ://domain.com

they would all go to => https://www.domain.com

If someone can point me in the right direction, that would be great!

Best Answer

  • This worked for me, you can test it on my domain if you like.

    <VirtualHost *:80>
        ServerName freesoftwareservers.com
        ServerAlias *.freesoftwareservers.com
        Redirect / https://www.freesoftwareservers.com/
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerName freesoftwareservers.com
        ServerAlias *.freesoftwareservers.com
    

    http://domain.com

    http://www.domain.com

    https://domain.com

    I had issues re-implementing this, and found that if I deleted 000-default.conf symlink from /sites-enabled it worked. Not sure whats the deal, but I have 0 other DNS records on the DNS side and my Apache does all the redirecting and it works.

  • Related Question