How To Redirect www and http to https

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. is the best url you can have unless you have subdomains in which case www may become a necessity.


Type and in browser one by one and it should redirect to

Don’t forget to replace 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.

WordPress Address (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]

# 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
# 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.


    1. Refresh the page, if it still doesn’t work close the browser and restart. Try in other browser. It should work unless you have something else installed which prevents it from redirecting correctly.

  1. Hi Ajay,
    I’ve a WordPress blog and I’ve used your code above to redirect www to https:// but it’s not working. http:// to https:// is working but not www to https:// how to fix it?

    1. I suspect that you don’t have an entry for www in your DNS. You can quickly check it by ping, if it doesn’t ping then your A record is missing for www. In your DNS Editor add A record for www and your IP address. Once this is added and resolved it will work. Also check if you have any entry in /etc/hosts file, if it is there comment it out. You can also do a DNS check for http://www.yourdomain,.com on if it resolves then it should work.

Leave a comment

Your email address will not be published.