Redirect From HTTP to HTTPS

February 22, 2016 28.2k views
Applications Let's Encrypt PHP

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://%{HTTPHOST}%{REQUESTURI}

  1. Redirect PHP Function

< ?php
function redirectTohttps() {
if($SERVER['HTTPS']!=”on”) {
$redirect= “https://”.$
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.

  • If you can, use the virtual host in terminal(command line) of your server, and change "Redirect Permanent" to the https version.. and then it will be redirected with entered as http.. or www... etc

  • Just add this into apache site config and change the domain.

    <VirtualHost *:80>
        ServerName example.com
        Redirect / https://example.com/
7 Answers

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
RewriteEngine on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

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
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

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

Hallo ryanpq,

I have already the two configuration files you mentioned and I ve enabled a2enmod rewrite but still not redirecting HTTP
When I compared your configuration files with mine, the <VirtualHost *:443> is the same.but

I have for example in <VirtualHost *:80>
ServerName sub.example.org -----my conf-->RewriteCond %{SERVER_NAME} =sub.example.org

do you think that I should change it



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;

Please teach me https to http
my website :
دانلود آهنگ : https://behmusic.com/

plz tell after go to https speed open the site low are this right?

Have another answer? Share your knowledge.