Force Redirect HTTP to HTTPS and WWW with HTACCESS

After installing SSL (HTTPS) for your website or blog, you should direct all traffic to the HTTPS version of the URLs domain for obvious reasons (Security, SEO, and the like). Here are the simple steps to automatically forward HTTP to HTTPS, and troubleshooting instructions.

We will assume your website is hosted on a Linux/Unix server and you can use an HTACCESS file. If you don’t know what that is, better go find someone (or ask us) who can quickly get the task done for you.

Automatically Redirect from HTTP to HTTPS:

In the root directory of your website domain, you may already have a file named “.htaccess” and may have some code within it.
You will need to add the following code snippet to the existing file, preferable at the beginning of the file. [Backup the file first!]
Code to paste:

# Automatically redirect HTTP to HTTPS -reference:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

IfModule mod_rewrite.c checks if mod_rewrite (required for this purpose) module is available.

There can be multiple “RewriteEngine On” statements in the file. One instance of that is all that is needed. Repetitions will are redundant, but aren’t a problem.

RewriteCond is the condition to meet. Our condition here means https is off.

RewriteRule is the rule where you specify what to do. We’re specifying to 301-redirect to an address where we’ve specified the first part of the URL and rest will be appended automatically from the request.
It’s possible to automatically grab and use the website domain name rather than hard-coding it in the sample above. However, differently configured servers can cause errors and we don’t want to take the chance.

R=301 means a 301 Redirection.

L means this is the last rule and no other rules should be processed. Though in this example, the same HTACCESS file and further rules will be applied post redirection.

Automatically Redirecting to WWW or non-WWW URL

You might like your website URLs always accessed as rather than just; or vice versa.
You can add an extra condition to check and force www or non-www.

Redirect naked domain to WWW and HTTPS:

# Automatically redirect to HTTPS and WWW -reference:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteRule .*{REQUEST_URI} [R=301,L]

Make sure you change the RewriteRule .*{REQUEST_URI} [R=301,L] to your website domain accordingly.

Notice we now have two conditions with an OR in between. So, the rule will redirect users if any of those conditions match.

Redirect to non-WWW and HTTPS:

# Automatically redirect HTTPS and non-WWW -reference:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule .*{REQUEST_URI} [R=301,L]

That’s it. Go ahead and check by typing different versions of the URLs. If everything works as expected, you’re all set and can skip the troubleshooting section below. However, read for a better understanding of things involved. It can help prevent issues in the future.


HTACCESS changes not reflecting

Most of the time it’s something wrong you’ve done in the HTACCESS code. However, use a clean browser or do a hard-refresh if there are 301 redirects involved.

HTACCESS changes not reflecting for a specific sub-directory or folder

It appears like a bug, but it’s probably not. If the sub-directory has its own HTACCESS file, that will be the ONLY HTACCESS file to be honored. To allow execution of rewrite rules from htaccess from parent folder, you need to explicitly allow it. Instead of that, just go ahead and make the changes again in the sub-directory. You will notice this if the root directory has WordPress and within that you’ve installed something like PHPBB in a sub-directory.

Weird-looking URL’s

It should not happen with the above example as we’re hard-coding the website domain in the rule. However, if you have tried code from elsewhere, consider hard-coding certain pieces; and you can eliminate the issues.

Too Many Redirects

Occasionally encountered if there’s another system that also attempts to modify the URL. This is very common with WordPress sites. The solution is easy. Go to your WordPress admin screen, then Settings -> General and make sure these two entries match your desired URL format (i.e. the HTTPS and WWW pieces):

How to auto-redirect to HTTPS with IIS on Windows Hosting

HTACCESS on Windows Hosting is handled in a different way. In fact, it’s not even HTACCESS.

On IIS server, you will need to download and install the URL Rewrite Module which would enable features very similar to HTACCESS on Unix/Linux hosting.

Need Help?

Feel free to drop a comment and I’ll respond.

61 Comments on “Force Redirect HTTP to HTTPS and WWW with HTACCESS”

  1. Thanks and I want to add some troubleshooting steps here.

    If you’re deleting lines from the code make sure you edit it the correct way, for example, I removed the line:
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    from a code block.

    And my condition then became:
    RewriteCond %{HTTPS} off [OR]

    The empty [OR] operator matched all the incoming requests and hence kept redirecting forever. That resulted in ‘too many redirects’ error.

    I spent more than an hour in the troubleshooting before I realized this silly mistake.

    Bottomline: If you have never edited htaccess file in the past, use the complete code blocks from the examples above. Don’t try to edit anything else.

  2. Great post however , I was wanting to know if you could write a litte more
    on this subject? I’d be very thankful if you could elaborate a little bit more.

  3. It’s truly very complex in this busy life to listen news on Television, thus I just use world wide web for that purpose, and take the latest

  4. I like the helpful information you provide in your articles.

    I will bookmark your blog and check again here frequently.
    I am quite certain I’ll learn lots of new stuff right here!
    Good luck for the next!

  5. Excellent post. I used to be checking continuously this
    weblog and I’m inspired! Very helpful info specifically the final section :
    ) I care for such info much. I was seeking this certain info for a very lengthy time.

    Thank you and best of luck.

  6. It’s amazing to go to see this web page and reading the views of all colleagues
    concerning this paragraph, while I am also eager of getting familiarity.

  7. I seriously love your site.. Pleasant colors & theme.
    Did you create this site yourself? Please reply
    back as I’m attempting to create my own personal site
    and would like to learn where you got this from or just what the
    theme is named. Appreciate it!

  8. oooh, pumpkin puree instead of pineapple sounds like my kind of swap. and doubling the carrots. and decreasing sugar. thanks for the heads up that all of this works! Emilie Cecil Lucier

  9. I will right away snatch your rss feed as I can not
    to find your e-mail subscription link or newsletter service.
    Do you’ve any? Please permit me recognise so that I
    may just subscribe. Thanks.

  10. Nice blog here! Additionally your web site lots up fast! What host are you the use of? Can I get your associate link on your host? I desire my site loaded up as quickly as yours lol Donny Burch Treharne

  11. I intended to create you the bit of remark to be able to thank you so much yet again on your spectacular tricks you have discussed on this website. This is so pretty open-handed of you to allow without restraint what many people could have marketed as an e book to end up making some money on their own, and in particular now that you might have tried it if you decided. Those tricks in addition worked as a easy way to know that some people have the same desire similar to my personal own to see good deal more with reference to this matter. Certainly there are lots of more pleasurable situations ahead for people who scan through your website. Nicky Chad Malloch

  12. I am often to blogging and i really appreciate your content. The article has really peaks my interest. I am going to bookmark your site and keep checking for new information. Hedvige Sherman Thurber

  13. Thanks for another magnificent article. The place
    else may just anyone get that kind of information in such an ideal means of writing?
    I’ve a presentation subsequent week, and I’m at the look for such info.

  14. Hello there. I found your website by means of Google at the same time as searching for a similar matter, your site got here up. It appears to be good. I have bookmarked it in my google bookmarks to come back then. Audy Deck Law

  15. Awesome!! It was great diving with you guys. Let me know when you are in AZ. I have some really unique dive spots that need to be explored. Great video! Great article! Mariel Wallis Negris

  16. Good day! This is my first visit to your blog!

    We are a team of volunteers and starting a new project in a community
    in the same niche. Your blog provided us beneficial information to work on. You have
    done a outstanding job!

  17. [url=]cialis price canada[/url] [url=]american online pharmacy[/url] [url=]buy albuterol 4mg tablets[/url] [url=]suhagra 100mg cheap[/url] [url=]where can you get clomid[/url] [url=]hydrochlorothiazide 136[/url] [url=]online pharmacy in germany[/url] [url=]buy tadalafil online[/url] [url=]trazodone 50mg capsules[/url] [url=]zofran canada[/url]

  18. [url=]doxycycline prescription[/url] [url=]how to use viagra[/url] [url=]canadian pharmacy 24[/url] [url=]where to get viagra for women[/url] [url=]generic female cialis[/url] [url=]albendazole medicine in india[/url] [url=]american pharmacy[/url] [url=]pharmaceuticals online australia[/url] [url=]can i buy cialis in mexico[/url] [url=]generic tadalafil 20mg[/url]

  19. [url=]where to purchase tadalafil[/url] [url=]nexium 24 coupon[/url] [url=]brand cialis canada[/url] [url=]zofran pill price[/url] [url=]how to get female viagra otc[/url] [url=]tenormin 50mg[/url] [url=]cheap kamagra 100mg[/url] [url=]malegra dxt without prescription[/url] [url=]buy cialis super active[/url] [url=]rx sildenafil tablets[/url]

  20. [url=]nexium otc best price[/url] [url=]dapoxetine buy us[/url] [url=]viagra online canada pharmacy[/url] [url=]order stromectol online[/url] [url=]buy tadalafil over the counter[/url]

  21. [url=]best wellbutrin generic[/url] [url=]cialis online pharmacy[/url] [url=]australia online pharmacy free shipping[/url] [url=]buy tadalafil 20[/url] [url=]synthroid 25 mcg[/url]

  22. [url=]generic viagra india[/url] [url=]online pharmacy[/url] [url=]albenza 200 mg medicine[/url] [url=]buy generic levitra from india online[/url] [url=]generic cialis pharmacy[/url] [url=]buy citalopram 10mg online[/url] [url=]online pharmacy in germany[/url] [url=]cleocin gel cost[/url] [url=]order tadacip[/url] [url=]order stromectol online[/url]

  23. [url=]pharmacy online track order[/url] [url=]sildenafil best price[/url] [url=]where can i buy erythromycin 333mg[/url] [url=]medrol 50 mg[/url] [url=]cymbalta canada generic[/url] [url=]best canadian pharmacy[/url] [url=]nexium 20 mg capsule[/url] [url=]where can i buy phenergan[/url] [url=]cialis 1800mg[/url] [url=]order proscar online[/url]

  24. [url=]erythromycin 500 mg tablet india[/url] [url=]albuterol 108[/url] [url=]prozac india price[/url] [url=]mypharmacy[/url] [url=]doxycycline 100 mg[/url]

  25. [url=]cymbalta discount prices[/url] [url=]medrol 8[/url] [url=]amoxicillin 875 mg buy online[/url] [url=]online med pharmacy[/url] [url=]fluoxetine 20 mg tablet[/url]

  26. [url=]buy propecia online without prescription[/url] [url=]best price generic wellbutrin[/url] [url=]buy prozac uk[/url] [url=]nexium 40 mg over the counter price[/url] [url=]tadalafil pills online[/url] [url=]clomid online no prescription[/url] [url=]buy cialis online without prescription[/url] [url=]viagra rx canada[/url] [url=]affordable pharmacy[/url] [url=]erythromycin[/url]

  27. [url=]brand name tenormin[/url] [url=]discount levitra canada[/url] [url=]ivermectin tablets[/url] [url=]abilify compare[/url] [url=]sildenafil pharmacy[/url]

  28. [url=]generic wellbutrin 450[/url] [url=]best online pharmacy[/url] [url=]cialis 5mg daily buy online[/url] [url=]viagra pills for sale[/url] [url=]synthroid 137 mcg price[/url]

  29. [url=]malegra 25 mg[/url] [url=]tretinoin prescription discount[/url] [url=]atenolol canada[/url]

  30. [url=]prednisone 20mg buy online[/url] [url=]female viagra sildenafil[/url] [url=]buy bactrim over the counter[/url] [url=]cheap arimidex[/url] [url=]can you buy cialis over the counter uk[/url]

Leave a Reply

Your email address will not be published. Required fields are marked *