Redirect to https and www

For WordPress

First step.

These days since everyone started giving free SSL en masse enabled by cPanel and Lets Encrypt a new problem has appeared. Redirection to https and to www.

Why is it important to redirect visitors, the simple reason is always the same, because of better searches on search engines, especially on Google. Google sees your site without www and with www as two different sites. The same goes for https and http. However, when it comes to http ie https, it is an even more important item, which is security and better ranking due to site protection and encryption.

How is redirection done?
Simply log in to cPanel, find the File Manager and in the public_html folder you have an .htaccess file. Yes with a full stop. If you don't see it, it means you don't see hidden files. Right upper corner "Settings" and tick Show Hidden files and folders. This will bring up the file.
You're going to edit.

If you use 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 under "RewriteEndgine On":

The next two lines are to redirect to https

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

The next two lines are to redirect to www

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

Which means the complete .htaccess file for redirecting 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 after this you can't open the site and you get something like: too many redirections, it means that something is trying to redirect the site to http, and with this change you are redirected to https, which entered an infinite loop.

If you use cloudflare and you set http there, that is the cause of 99% people, then on cloudflare change it to redirect to https and the problem will be solved.

For other sites

To see the .htaccess file in File Manager, click on Settings in the upper right corner and then tick Show hidden file and folders.
If the .htaccess file does not appear in your domain folder, then simply create it by clicking on the +File button in the upper left corner and enter .htaccess (yes with a dot at the beginning).

Next is to redirect to https

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

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

Next is to redirect to https

If you want to redirect to https and to www, then 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