Reroute Drupal 9 site from http to https using the .htaccess file

Submitted by Jhun Vert on Tue, 12/22/2020 - 22:07

Go to the root directory of Drupal 9 site and change the write permission of the .htaccess file to write enable.
sudo chmod 777 .htaccess

Open .htaccess with nano command
sudo nano .htaccess

Insert the following lines:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:jhun\.verterra\.me)?(.*)$ [NC]
RewriteRule (.*){REQUEST_URI} [L,R=301]


The above insertion is best placed below the redirection from WWW to without WWW.

Exit from nano and save the .htaccess file

Revert the permission
sudo chmod 644 .htaccess

Go to your browser and check if redirection from http to https works. You may need to flush all cache.