Question

Redirect From HTTP to HTTPS

Hey, I recently installed Lets Encrypt SSL, its successful however it wont redirect whenever its on http. So how to redirect all to https??

Here method I already tried but it seems wont working: 1 .htaccess

RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

  1. Redirect PHP Function

< ?php function redirectTohttps() { if($_SERVER[‘HTTPS’]!=”on”) { $redirect= “https://”.$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’]; header(“Location:$redirect”); } } ?>

  1. HTML Meta Tag

< meta http-equiv=“Refresh” content=“0;URL=https://www.example.com” />

Note*: This method works however it cause infinite loop and crash the server.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hey. letsencrypt’s auto option should do this for you so I am guessing it’s possible you do not have the rewrite module enabled. This can be fixed by running sudo a2enmod rewrite; sudo service apache2 restart

I have a fresh LAMP server I ran letsencrypt on the other day with a pretty standard configuration and redirects are working as expected so I’ll just share that config with you.

letsencrypt creates two configuration files if you opt for the redirect http to https option. The first is for the non-https (port 80) host.

<VirtualHost *:80>
 ServerName sub.example.org
 DocumentRoot /var/www/sub
 DirectoryIndex index.php
 <Directory /var/www/sub/>
 AllowOverride All
 Order Deny,Allow
 Allow from all
 </Directory>
RewriteEngine on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>

Then the file created for the SSL (port 443) host:

<IfModule mod_ssl.c>
<VirtualHost *:443>
 ServerName sub.example.org
 DocumentRoot /var/www/sub
 DirectoryIndex index.php
 <Directory /var/www/sub/>
 AllowOverride All
 Order Deny,Allow
 Allow from all
 </Directory>
SSLCertificateFile /etc/letsencrypt/live/sub.example.org/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/sub.example.org/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateChainFile /etc/letsencrypt/live/sub.example.org/chain.pem
</VirtualHost>
</IfModule>

  • domain and directory path changed to protect my un-launched service’s identity :)

Nginx:

server {
    listen 80;
    server_name .example.com;

    location / {
        rewrite ^(.*) https://example.com$request_uri;
    }
}

server {
    listen {IP}:443 ssl;
    server_name example.com;
    ssl_certificate     /etc/ssl/certs/example.com/example_com.crt;
    ssl_certificate_key /etc/ssl/certs/example.com/example.com.key;

    root "/var/www/example.com";
    error_log  "/var/log/nginx/error.example.com.log";

    location / {
        try_files $uri $uri/ index.html;
    }
}

Go to the General option under Settings in your WordPress dashboard. Now make sure the Wordpress Address URL and the Website Address URL are both https. If not, update the url to https and click on Save.