How to Force www and https in .htaccess

Add Expires Headers to WordPress .htaccess File

As a beginning blogger you may have an issue with proper https://www configuration.

In this article, I will show you how to force www and https in .htaccess file which is available on every Apache server.

At the end of this article, I will show a solution for those who use a hosting provider not based on Apache server such as Kinsta which is fully based on Nginx.


Apache vs Nginx


Before I tell you what is .htaccess file and how you can force https://www on your site, I have to mention about Apache and Nginx first.

Apache and Nginx are two types of web servers.

The first one of them was initially released in 1995, and the second one – in 2004.

In general, Nginx is considered to be more secure and powerful than Apache.

Furthermore, there’s also an interesting dependency between websites’ popularity and the types of servers chosen by the customers.

As you can see below, the more popular a website is, the more frequently it is hosted on Nginx server.

percentage of websites using Nginx and Apache


What Is .htaccess File


In short, .htaccess file is a configuration file of Apache server.

This file is located in a home directory of your website and it may contain tens or hundreds lines of code.


How To Check Your Current https www Redirection Settings


In order to check your current redirect settings, you should visit any 301 redirect checker and type in your domain name.

Here’s an example of redirects which are not properly set:

not properly set htaccess 301 redirect

and here’s a good one:

properly set htaccess https www 301 redirect


How To Force www And https In Case Of Almost Every Hosting Provider


In order to force www and https in htaccess file, you should go through the following steps:

1. Log into your WordPress dashboard.

2. Go to WP File Manager (install and activate it first if you haven’t done that yet).

htaccess edit 01

3. Right-click .htaccess file (which is by default located in public directory) and choose Code Editor.

4. Copy this block of code:

<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

and paste it at the beginning of your .htaccess file.

htaccess edit 02

5. After saving changes and closing the file, you should use Search & Replace plugin in order to update all the links in your database.

In my case, I needed to search for:


and replace them with

You just need to change my domain name to yours and do the same what I’m describing here.

Updating links in WordPress database with Search & Replace plugin

As opposed to what majority of guides can recommend you, you don’t have to use Dry Run.

You will even finish your job faster if you do not check that option.

6. Clear the cache on your server and your CDN.

For more information in this topic, go to this article.


How To Force www And https In SiteGround


In case of SiteGround, you just need to use SG Optimizer plugin.

SiteGround - SG Optimizer - fix mixed content issue

Alternatively, you can use the method described in the previous section.

However, SiteGround is a service based on both Apache and Nginx.


How To Force www And https In Kinsta


Kinsta is not a service based on Apache (it is based on Nginx only) which means it does not contain .htaccess file.

In this article, I was going to show you how to force https and www in .htaccess file.

Since Kinsta is not based on Apache, I recommend you visiting this link in order to check how to properly set https://www if you host your site at Kinsta.