quma
quma

Reputation: 5733

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?

Upvotes: 0

Views: 328

Answers (2)

Allan Joseph
Allan Joseph

Reputation: 145

Since you are using Ubuntu with Apache. LetsEncrypt automatically installs and does the configuration for apache for SSL. If you want to do a manual configuration follow the below.

File name-xy.conf should look like this:

<VirtualHost *:80>
    ServerName mywebsite.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ProxyRequests off
    ProxyPreserveHost On
    <Location / >
        ProxyPass "ajp://localhost:9090/"
        ProxyPassReverse "ajp://localhost:9090/"
    </Location>
    #Add the below Lines
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =mywebsite.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
</VirtualHost>

File name-xy-ssl.conf should look like this:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName mywebsite.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    # 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>
</IfModule>

Restart your apache server sudo service apache2 restart and clear your browser cache and history to take effect.

Upvotes: 0

user11553043
user11553043

Reputation:

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

Upvotes: 1

Related Questions