Forcing the SSL and WWW / non-WWW with HTACCESS

If you have just installed an SSL certificate on your website, then you might be looking to force the SSL so your website visitors will be able to access your website through secure URLs only. Moreover, you might be looking to force WWW too as it is a good practice to always have a single version of your website URL and all others to (301) redirect to it.

Let’s have a look over a simple and straight-forward HTACCESS rule that lets you achieve both of these two goals at once. Add below rule to the top of your website’s HTACCESS file is going to force SSL as well as it will always ensure that your website is loaded over www version of the domain.

Hint: Replace yoursite and com in third line and yoursite.com in the fourth line with your own values.

On the other hand, if you want to force the SSL without WWW, then add below rule to your website’s HTACCESS file.

The two blocks of rules ensure that your website is always loaded over HTTPS and non-www version of the domain. You don’t need to modify anything here. Just copy and paste the rules in your website’s HTACCESS file.

I hope these rules helped you in achieving the desired results. If you have any questions or suggestions, let’s talk in comments.

2 Comments

Leave a reply