1

Google has started punishing non https sites both in SEO and their browsers. So I guess we must make sure that all our clients are using https

enter image description here

Assuming that I have a domain example.com I would like to redirect all the variations of this domain to the https version.

There can be six variations example.com, www.example.com, http://example.com, http://www.example.com, https://example.com, https://www.example.com.

I want to make sure that all these variations redirect to https://www.example.com.

2

example.com is the naked version of www.example.com. In the settings for the domain, add an A name pointing to IP of your server.

enter image description here

Add a C Name that points to the www version at the same IP.

enter image description here

If you are running Apache, in the virtual host settings add both Servername and ServerAlias.

<VirtualHost *:80>                                                                                                          
        ServerName example.com
        ServerAlias www.example.com 

This will make sure that your server listens both the naked domain and normal domains and points them to you code folder for execution.

Make sure the .htaccess file contains the following lines.

  RewriteEngine on

  # Set "protossl" to "s" if we were accessed via https://.  This is used later
  # if you enable "www." stripping or enforcement, in order to ensure that
  # you don't bounce between http and https.
  RewriteRule ^ - [E=protossl]
  RewriteCond %{HTTPS} on
  RewriteRule ^ - [E=protossl:s]

This code just sets a flag called protossl, if you have visited using https. This will ensure that you don't have infinite redirections between http and https as mentioned in the comments.

Comment out all other settings related to http and https redirection, and add the following lines.

#
# Rewrite http(s)://example.com to https://www.example.com
#
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#
# Rewrite http://www.example.com to https://www.example.com
#
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This code was provided by https://www.drupal.org/u/mdrescher on https://www.drupal.org/forum/support/post-installation/2018-04-15/forcing-to-https#comment-12723535.

These changes will make sure that all six variations example.com, www.example.com, http://example.com, http://www.example.com, https://example.com, https://www.example.com are redirection https://www.example.com

Not the answer you're looking for? Browse other questions tagged or ask your own question.