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: bloghow.com
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

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 www.yoursite.com rather than just yoursite.com; 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: bloghow.com
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
</IfModule>

Make sure you change the RewriteRule .* https://example.com%{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: bloghow.com
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]
</IfModule>

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.

Troubleshooting:

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.
    Cheers!

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

  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=https://packcialis.com/]cialis price canada[/url] [url=https://dpptables.com/]american online pharmacy[/url] [url=https://ventolinha.com/]buy albuterol 4mg tablets[/url] [url=https://tabsildenafil.com/]suhagra 100mg cheap[/url] [url=https://tkmeds.com/]where can you get clomid[/url] [url=https://hydrochlorothiazidehctz.com/]hydrochlorothiazide 136[/url] [url=https://stoppainrem.com/]online pharmacy in germany[/url] [url=https://cialisoff.com/]buy tadalafil online[/url] [url=https://oggpharm.com/]trazodone 50mg capsules[/url] [url=https://ondansetronzofran.com/]zofran canada[/url]

  18. [url=http://antibactabs.com/]doxycycline prescription[/url] [url=http://iviagrabuy.com/]how to use viagra[/url] [url=http://efftablets.com/]canadian pharmacy 24[/url] [url=http://viagrareg.com/]where to get viagra for women[/url] [url=http://cialisdt.com/]generic female cialis[/url] [url=http://albendazolealbenza.com/]albendazole medicine in india[/url] [url=http://stoppainrem.com/]american pharmacy[/url] [url=http://zmedsearch.com/]pharmaceuticals online australia[/url] [url=http://cialisdisp.com/]can i buy cialis in mexico[/url] [url=http://cialisuno.com/]generic tadalafil 20mg[/url]

  19. [url=http://howtocialis.com/]where to purchase tadalafil[/url] [url=http://nexiumesomeprazole.com/]nexium 24 coupon[/url] [url=http://packcialis.com/]brand cialis canada[/url] [url=http://ondansetronzofran.com/]zofran pill price[/url] [url=http://xxlviagra.com/]how to get female viagra otc[/url] [url=http://tenorminmed.com/]tenormin 50mg[/url] [url=http://viagrabtab.com/]cheap kamagra 100mg[/url] [url=http://malegrapill.com/]malegra dxt without prescription[/url] [url=http://cialismedication.com/]buy cialis super active[/url] [url=http://tabsildenafil.com/]rx sildenafil tablets[/url]

  20. [url=http://nexiumesomeprazole.com/]nexium otc best price[/url] [url=http://drugtadalafil.com/]dapoxetine buy us[/url] [url=http://enviagra.com/]viagra online canada pharmacy[/url] [url=http://ivermectinpharmacy.com/]order stromectol online[/url] [url=http://cialisopp.com/]buy tadalafil over the counter[/url]

  21. [url=http://imqpills.com/]best wellbutrin generic[/url] [url=http://cialisorder.com/]cialis online pharmacy[/url] [url=http://skincaretab.com/]australia online pharmacy free shipping[/url] [url=http://ycialis.com/]buy tadalafil 20[/url] [url=http://clppharm.com/]synthroid 25 mcg[/url]

  22. [url=http://btviagra.com/]generic viagra india[/url] [url=http://tbbstore.com/]online pharmacy[/url] [url=http://albendazolealbenza.com/]albenza 200 mg medicine[/url] [url=http://reallevitra.com/]buy generic levitra from india online[/url] [url=http://cialisoff.com/]generic cialis pharmacy[/url] [url=http://imqpills.com/]buy citalopram 10mg online[/url] [url=http://zmedsearch.com/]online pharmacy in germany[/url] [url=http://cleocinclindamycin.com/]cleocin gel cost[/url] [url=http://cialismedication.com/]order tadacip[/url] [url=http://ivermectinpharmacy.com/]order stromectol online[/url]

  23. [url=http://antibioticspt.com/]pharmacy online track order[/url] [url=http://sildenafilhow.com/]sildenafil best price[/url] [url=http://grpills.com/]where can i buy erythromycin 333mg[/url] [url=http://medrolmedication.com/]medrol 50 mg[/url] [url=http://cymbaltapill.com/]cymbalta canada generic[/url] [url=http://conmstore.com/]best canadian pharmacy[/url] [url=http://nexiumesomeprazole.com/]nexium 20 mg capsule[/url] [url=http://ddvrx.com/]where can i buy phenergan[/url] [url=http://cialisdisp.com/]cialis 1800mg[/url] [url=http://egthealth.com/]order proscar online[/url]

  24. [url=https://optablets.com/]erythromycin 500 mg tablet india[/url] [url=https://ventolinha.com/]albuterol 108[/url] [url=https://ntmhealth.com/]prozac india price[/url] [url=https://efftablets.com/]mypharmacy[/url] [url=https://antibactabs.com/]doxycycline 100 mg[/url]

  25. [url=http://cymbaltapill.com/]cymbalta discount prices[/url] [url=http://medrolmedication.com/]medrol 8[/url] [url=http://antibioticspt.com/]amoxicillin 875 mg buy online[/url] [url=http://zmedsearch.com/]online med pharmacy[/url] [url=http://oggpharm.com/]fluoxetine 20 mg tablet[/url]

  26. [url=https://egthealth.com/]buy propecia online without prescription[/url] [url=https://buyimedications.com/]best price generic wellbutrin[/url] [url=https://ntmhealth.com/]buy prozac uk[/url] [url=https://nexiumesomeprazole.com/]nexium 40 mg over the counter price[/url] [url=https://cialisdisp.com/]tadalafil pills online[/url] [url=https://tkmeds.com/]clomid online no prescription[/url] [url=https://cialisorder.com/]buy cialis online without prescription[/url] [url=https://viagrabtab.com/]viagra rx canada[/url] [url=https://pllzshop.com/]affordable pharmacy[/url] [url=https://tbbstore.com/]erythromycin[/url]

  27. [url=http://tenorminmed.com/]brand name tenormin[/url] [url=http://reallevitra.com/]discount levitra canada[/url] [url=http://ivermectinpharmacy.com/]ivermectin tablets[/url] [url=http://imqpills.com/]abilify compare[/url] [url=http://viagraret.com/]sildenafil pharmacy[/url]

  28. [url=http://imqpills.com/]generic wellbutrin 450[/url] [url=http://pfhhealth.com/]best online pharmacy[/url] [url=http://packcialis.com/]cialis 5mg daily buy online[/url] [url=http://xxlviagra.com/]viagra pills for sale[/url] [url=http://clppharm.com/]synthroid 137 mcg price[/url]

  29. [url=https://malegrapill.com/]malegra 25 mg[/url] [url=https://adatabs.com/]tretinoin prescription discount[/url] [url=https://tenorminmed.com/]atenolol canada[/url]

  30. [url=https://skincaretab.com/]prednisone 20mg buy online[/url] [url=https://viagrareva.com/]female viagra sildenafil[/url] [url=https://optablets.com/]buy bactrim over the counter[/url] [url=https://anastrozolearimidex.com/]cheap arimidex[/url] [url=https://rlcialis.com/]can you buy cialis over the counter uk[/url]

Leave a Reply

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