If your site is in HTTPS, you must set up a 301 redirect from any URL in HTTP to its equivalent in HTTPS. Ditto for the reverse! This article explains how to do it (in Apache, IIS, Lighttpd and Nginx).

If you have just switched your site from HTTP to HTTPS , don’t forget that you must redirect all the old URLs (in HTTP) to the new ones (in HTTPS).

This also applies to a site which from the start is in HTTPS, because it is likely that some will make links (without realizing it) to the site in HTTP.

Manage redirection at VirtualHost level on Apache

A good way is to set up the redirect directly in the configuration file that defines your VirtualHost. This file can be in various locations depending on the system you are using (see here for Apache ).

If you want to redirect any URL in HTTP to the equivalent URL in HTTPS, add this in the virtualhost which corresponds to HTTP:

Permanent redirect / https://www.example.com/

(remember to replace with the name of your site)

If you really want to use RewriteRule(not a good idea at first, see the comments):

RewriteEngine on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

This works for all URLs managed by the virtualhost , with or without a subdomain.

Obviously, if you are interested in the opposite, if you want to redirect any URL in HTTPS to the equivalent URL in HTTP, add this in the virtualhost which corresponds to HTTPS:

Permanent redirect / http://www.example.com/

(remember to replace with the name of your site)

If you really want to use RewriteRule(not a good idea at first, see the comments):

RewriteEngine on
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Manage redirection in an .htaccess file

You can also configure these redirects in a file .htaccessat the root of your site.

Redirect HTTP to HTTPS

In this case, if you want to redirect any URL in HTTP to the equivalent URL in HTTPS, add this in the .htaccess file at the root of your site:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The condition  %{HTTPS} !=on means “the URL is requested without the HTTPS protocol”. You can also test RewriteCond %{HTTPS} off

On some servers / hosts, it does not work to test this variable. You must then go through the port number, knowing that HTTP is on port 80 and HTTPS is on port 443.

On OVH, here is the code to use:

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

On Gandi (Simple Hosting instance), here is the code to use:

RewriteEngine on
RewriteCond %{REQUEST_SCHEME} =http
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

(be careful not to put a space between = and http)

Redirect HTTP to HTTPS and manage www at the same time

You know, you should not create duplicate content by leaving your site accessible both with and without www. We must redirect from the wrong to the right structure. It’s even better to do both redirects in one go to avoid chain redirects.

If the official version of your site contains the www subdomain, try this code (replace the example.com domain name with your own):

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^.*$ https://www.%1%{REQUEST_URI} [L,NE,R=301]

Redirect HTTPS to HTTP

If, on the other hand, you want to redirect any URL in HTTPS to the equivalent URL in HTTP, add this to the file .htaccessat the root of your site:

RewriteEngine on
RewriteCond %{HTTPS} on
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

On OVH, here is the code to use in the file .htaccessat the root of your site to block your site in HTTP:

RewriteEngine on
RewriteCond %{SERVER_PORT} 443
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

HTTP to HTTPS redirection with other servers

Redirection to HTTPS with an IIS server

For IIS 7, IIS 8 and later, explanations are provided here by Microsoft or  in this article  . Basically, there are 3 methods (from most to least recommended):

  • IIS redirection with the URL Rewrite module
  • IIS redirection with the default document (default.asp)
  • IIS redirection with the HTTP Redirect module

Redirection to HTTPS with a Lighttpd server

For  Lighttpd , here’s how ( source ):

Example 1, we redirect everything:

$HTTP["scheme"] == "http" {
    # capture vhost name with regex conditiona -> %0 in redirect pattern
    # must be the most inner block to the redirect rule
    $HTTP["host"] =~ ".*" {
        url.redirect = (".*" => "https://%0$0")
    }
}

Example 2, for a single specific URL:

$HTTP["scheme"] == "http" {
    $HTTP["host"] =~ ".*" {
        url.redirect = ("^/phpmyadmin/.*" => "https://%0$0")
    }
}

Example 3, for a vhost and a single specific URL:

$HTTP["scheme"] == "http" {
    $HTTP["host"] == "sth.example.com" {
            url.redirect = ("^/phpmyadmin/.*" => "https://sth.example.com$0")
    }
}

You can also use tests of this type:

if $HTTP["scheme"] == "https"

Redirection to HTTPS with an Nginx server

For Nginx , here is the code to put in your configuration file ( source ):

server {
	listen 80 default_server;
	listen [::]:80 default_server;
	server_name _;
	return 301 https://$host$request_uri;
}

or a slightly more robust code in which we define which hostnames are concerned:

server {
	listen 80 default_server;
	listen [::]:80 default_server;
	server_name example.com www.example.com;
	return 301 https://$server_name$request_uri;
}

you will also need a setting for port 443 (you will need other details, hence the […]):

server {
	listen 443 ssl;
	server_name example.com www.example.com;
	[...]
}

Important Notes

  • in case of other RewriteRules, it is necessary to take care of the order and the conflicts: it is up to you to check and test
  • in theory mod_rewrite is not the best way to proceed ( see here ) although in practice it is a detail
  • things can get complicated if the server manages several different domains in https
  • it will also be necessary to think of combining with the management of the www
  • if you have url rewriting , check that the rules do not interfere (you never know)

Questions answers

How to redirect HTTP to HTTPS on WordPress?

WordPress does not offer a way to manage the switch to HTTPS directly in the backoffice. You can test WordPress plugins like Really Simple SSL  but not having tested them I have no opinion. 

If you have a dedicated server, manage the redirects in your server config (VirtualHost under Apache for example).

Otherwise, follow the guidelines above to handle redirects in the .htaccess file.

How to redirect HTTP to HTTPS on Joomla?

The above methods work, but you can also go to the settings, section “Server settings”> “Force HTTPS” and choose “Administration and site”.

HTTPS redirection on Prestashop

To automatically redirect the URLs of a Prestashop site using HTTPS, follow this tutorial  .

HTTPS redirection on Drupal

Ditto: to make an automatic HTTPS redirection of a site under Drupal, follow this tutorial  (in English).

How to redirect HTTP to HTTPS at OVH?

If you have a dedicated OVH server, manage redirects in your server config (VirtualHost under Apache for example). Otherwise, on an OVH shared server, as indicated in the article, the code for redirects in .htaccess is slightly different from the standard. See the details above.

Is the redirection automatic?

As long as you do not add the redirect codes, it will not be automatic and you will risk having your site duplicated (HTTP and HTTPS). This is not good for SEO

But once the codes are in place, any URL in HTTP will automatically be redirected to the equivalent in HTTPS.

Where can I find SSL certificates?

There are free ones (for example Let’s Encrypt) and many paid ones. Do a search here for example . Whatever the certificate, if it is up to date and correctly configured / installed, you will benefit from the green padlock (“secure site” / “secure site”).

Thanks.

5/5 - (2 votes)
Web

Tagged in:

,

About the Author

SAKHRI Mohamed

Founder & Editor

Passionate about the web, new technologies and IT, I share on easy-tutorials.com tutorials, tips, advice, online tools and software for Windows, Mac and Linux.

View All Articles