Question

How to redirect from http to https?

Posted June 18, 2019 993 views
ApacheUbuntu 18.04

Hi, I install my wordpress site on an ubuntu 18.04 droplet and after posting a few articles I proceed to install lestencrypt.

My https site works fine, my problem is when trying to enter with http it does not redirect.

This is the .httacces file from my site:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

RewriteCond %{HTTPS}  !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

And my 80 VirtualHost at /etc/apache2/sites-available (instead of example is my domain):

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/onemoreshot
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/onemoreshot/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com [OR]
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

And my 443 VirtualHost at /etc/apache2/sites-available (instead of example is my domain)

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /var/www/example/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>

I tried both, redirection by .httaccess and by VirtualHost.
I’m using google domain.

Thanks in advance

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hi there @juanmorschrott,

As you’re using WordPress for your website, it may be much easier to use a WordPress plugin to handle the HTTP->HTTPS rewrites instead of setting up yourself with rewrite rules.

I would suggest a plugin such as https://wordpress.org/plugins/really-simple-ssl/#installation to achieve this. You should be able to install it from your WordPress panel under the plugins menu.

Before installing it, I suggest removing your attempts to do the redirection from your htaccess & virtualhost files to avoid any possible issues caused by rule collisions etc.

Hope that helps!
- Matt.

I’ve been looking through the provided annotations and I can’t seem to find one that toggles the ‘Http to Https’ redirect option on DO load balancers?

( I went through this file: https://github.com/digitalocean/digitalocean-cloud-controller-manager/blob/master/do/loadbalancers.go )

At the moment my setup is like this ( terraform based ) :

...
resource "kubernetes_service" "srv" {
        metadata {
                name = "..."
                namespace = "${var.m_namespace}"
                annotations {
                        "service.beta.kubernetes.io/do-loadbalancer-protocol" = "http"
                        "service.beta.kubernetes.io/do-loadbalancer-algorithm" = "round_robin"
                        "service.beta.kubernetes.io/do-loadbalancer-tls-ports" = "443"
                        "service.beta.kubernetes.io/do-loadbalancer-certificate-id" = "${var.m_certificate_id}"
                }
        }

        spec {
                selector {
                        app = "..."
                }
                port = [
                        {
                                name = "http"
                                port = 80
                                protocol = "TCP"
                                target_port = 8080
                        }, {
                                name = "https"
                                port = 443
                                protocol = "TCP"
                                target_port =  8080
                        }
                ]

                type = "LoadBalancer"
        }
}
...

What would be the expected annotations for this ( if it is available)? Or what would be an alternative? For now, I’m making the adjustment manually once the load balancer has been provisioned, but that’s really not the expected way of working for me :/

Loving from Rohit Manglik.

edited by MattIPv4
Submit an Answer