apache2lets-encryptacme

Apache2 Rewrite/Redirect from http to https


I use apache2 on Ubuntu and acme with letsencrypt in order to secure my website. I also want to redirect from http://mywebsite.com to https://mywebsite.com but this does not work with the following config:

File name-xy.conf looks like this:

<VirtualHost *:80>
    ServerName mywebsite.com

    ProxyRequests off
    ProxyPreserveHost On
    <Location / >
        ProxyPass "ajp://localhost:9090/"
        ProxyPassReverse "ajp://localhost:9090/"
    </Location>
</VirtualHost>

File name-xy-ssl.conf looks like this:

<VirtualHost *:80>
    ServerName mywebsite.com

    RewriteEngine on
    #RewriteCond %{SERVER_NAME} =www.mywebsite.com [OR]
    RewriteCond %{HTTPS} =mywebsite.com
    RewriteRule (.*) https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

 # forward ORDS requests to tomcat
 <VirtualHost *:443>
    ServerName mywebsite.com

    # SSL certificates settings
    #Include /etc/apache2/conf-enabled/options-ssl-apache.conf
    SSLCertificateFile /etc/apache2/ssl/mywebsite.com/fullchain.cer
    SSLCertificateKeyFile /etc/apache2/ssl/mywebsite.com/mywebsite.com.key
    SSLCertificateChainFile /etc/apache2/ssl/mywebsite.com/ca.cer

    ProxyRequests on
    ProxyPreserveHost On
    <Location / >
        ProxyPass "ajp://localhost:9090/"
        ProxyPassReverse "ajp://localhost:9090/"
    </Location>
</VirtualHost>

With this config I will navigate to the default apache2 homepage, https://mywebpage.com works fine. What is wrong in this config in order to redirect from http://mywebsite.com to https://mywebsite.com automatically?


Solution

  • To handle this situation, I have my http vhost as follows. I don’t think it requires enabling any new modules either, just add the redirect statement like the last line :)

    http-config