First Rant..
Since the men in tower dictated that SSL will become norm for websites we common men are forced to follow their dogma and do as they dictate us to do.
Not too far in the future, non ssl domains may be blocked by the men in tower all together. So it’s better you start having SSL on your website and redirect http and www to https.
Redirect http & www to https on Linux and Apache
Copy following code towards the top of .htaccess file in your web root and all job will be taken care of.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
Why to redirect www to https?
It’s simply because you don’t want users to type r extra letters to reach your website. https://yourdomain.com is the best url you can have unless you have subdomains in which case www may become a necessity.
Test
Type http://yourdomain.com and http://www.yourdomain.com in browser one by one and it should redirect to https://yourdomain.com.
Don’t forget to replace yourdomain.com with your actual domain 😉
How to add redirect on a self hosted WordPress blog?
Firstly you should fix the url of your blog. From WordPress Dashboard go to Settings -> General
Enter your url with https:// in it. This will be your target and default URL.
Next is to redirect www and http to https. If this is a brand new WordPress installation then you can simply copy and paste above code in .htaccess towards the top.
Here is what a typical .htaccess file will look like after adding redirect code in it. I’ve highlighted redirect code in bold.
Don’t forget to test your blog urls after updating .htaccess code.
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
#### Redirect www, http to https ##########
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www. [NC]
RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
######################
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
If your WordPress blog is not fresh then there are chances that some of the plugins may have been already taking care of the redirect. To check it type http, www urls and if it is already redirecting to https then it’s all good. If not then you can copy and paste code as described above.