How to redirect all traffic to HTTPS on your WordPress blog using .htaccess file

To be honest, this article is written for me because I Google this every time when I install an SSL certificate to the WordPress site.

This is a very important thing to do right because if this redirection is not done right you can lose all your Google or any other traffic!

First thing you need is access to your hosting server. There you need to find .htaccess file which usually is in the root folder of your WordPress installation. You can use the file manager that your hosting provides.

On the next image, you can see how the root of your WordPress installation looks, and I highlighted a .htaccess file for you.

Location of a .htaccess file in WordPress installation
Location of a .htaccess file in WordPress installation

You can edit a .htaccess file (or any file from server) in a few ways. One way would be that you do it in file manager by selecting a file and clicking on edit (or right-click on the file and then edit). Other ways include using an FTP or SSH.

You can use whatever you want, the main goal here is open and edit the content of the .htaccess file. The default .htaccess file in WordPress installation will look like this:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

This is what we need to add so that our server redirects all sites to https:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

And this is how your final .htaccess file should look like:

# BEGIN WordPress

RewriteEngine On 
RewriteBase / 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
RewriteRule ^index.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 

# END WordPress

Now save the file and test your site. Don’t test just front page, test also by opening HTTP versions of some posts or any other pages and see if you are going to be redirected to the HTTPS version of that same page.

I hope that this post helped you and you succeed. In case you still have a problem check this links for solutions:

Published by Stjepan

Just another software developer

Leave a comment

Your email address will not be published. Required fields are marked *