How to Force www and https in .htaccess

How to Force www and https in .htaccess

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 the 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 their owners.

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

percentage of websites using Nginx and Apache


What Is .htaccess File


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

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


How To Check Your Current https www Redirection Settings


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 https www 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 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 as what I’m describing here.

Updating links in WordPress database with Search & Replace plugin

As opposed to what the 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 on this topic, go to this article.


How To Force www And https In SiteGround


In the case of SiteGround, you can try to force https and www using their SG Optimizer plugin.

There are two switches created for this purpose – Enable HTTPS and Fix Insecure Content.

SiteGround - SG Optimizer - fix mixed content issue

However, in my case, it didn’t work anytime I tried it, so I had to use the method described in the previous section.

Besides, you should know that SiteGround is a service based on both Apache and Nginx servers (which is not especially beneficial from the security and performance points of view).


How To Force www And https In Kinsta


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

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

Therefore, Kinsta will allow you to set https www faster and easier properly.

Besides, you will easily add expires headers to your site, and you will achieve spectacular results in all website speed tests available online.

More information on these topics will be available here in the coming weeks.