Redirection to https and www

For WordPress

Why is it essential to redirect visitors? The reason is always the same: better search engine rankings, especially on Google. Google views your site with and without www as two different sites. The same applies to https and http. However, when it comes to http and https, it is an even more critical factor: security and better ranking due to site protection and encryption.

How is redirection done?

First you need from your website dashboard to go Settings > General and change site url to https and www and save. If you skip this step you may will get error “too many redirects”

Simply log into cPanel, find File Manager, and locate the .htaccess file in the public_html folder. Make sure to include the dot at the beginning. If you don’t see it, it means you don’t see hidden files. In the top right corner, click “Settings” and check the box for Show Hidden files and folders. The file will appear.

Click edit.

If you are using WordPress, your .htaccess file should look like this:

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

What you need to do is add the following below “RewriteEngine On”:

The next two lines are for redirecting to https

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

The next two lines are for redirecting to www

 RewriteCond %{HTTP_HOST} !^www\. [NC]
 RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

This means the complete .htaccess file for redirection to https and www should be:

# BEGIN WordPress
RewriteEngine On
 RewriteCond %{HTTPS} off
 RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 RewriteCond %{HTTP_HOST} !^www\. [NC]
 RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 RewriteBase /
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
# END WordPress
 

That’s all!

If you can’t open the site after this and get something like “too many redirections,” it means that something is trying to redirect the site to http, but you have changed the redirection to https, causing an infinite loop.

If you use Cloudflare and have set up http there, that is the cause for 99% of people. In that case, change the setting on Cloudflare to redirect to https, and the problem will be solved.

For other websites

To see the .htaccess file in File Manager, click on Settings in the top right corner and then check the Show hidden file and folders box.

If the .htaccess file does not appear in your domain folder, simply create it by clicking the +File button in the top left corner and entering .htaccess (with a dot at the beginning).

If you want redirection to https only without www, save the following code in your .htaccess file:

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

If you want redirection to both https and www, save the following code in your .htaccess file:


RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Scroll to Top