• Skip to primary navigation
  • Skip to main content
  • Skip to footer

WPJohnny

WordPress Performance Guides and Reviews

  • Start a WordPress site
  • Hosting
  • Themes
  • Plugins
  • Blogging
  • Marketing

301 Redirects to HTTPS (with and without WWW)

WordPress hosting Apr 14, 2018 by Johnny 19 Comments

I’ve given examples for both “www” and also “without www”. I gave neutral code that works with any site. If you want, you could also replace “%{HTTP_HOST}%” with your domain name. I only have rewrites for Apache htaccess right now. (NGINX servers can try this.)

Add this code above the #BEGIN WordPress line in your htaccess. 

Method #1

This one uses less code but has more redirects when you use page tools. It works well but perhaps slightly less efficient than the second. (I recommend this method for everyone using Swift Performance plugin.)

WITHOUT www (all visits go to “https://domain.com”):

#301 https redirects to without WWW
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

WITH www (all visits go to “https://www.domain.com”):

#301 https redirects to with WWW
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Method #2

My new favorite method, thanks to Nazar Hotsa who’s researched every method and shared this with me. Awesome community guy with tons of enterprise webhosting experience.

WITHOUT www (all visits go to “https://domain.com”):

# BEGIN Redirects
RewriteEngine On
# 301 redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# 301 redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END Redirects

WITH www (all visits go to “https://www.domain.com”):

# BEGIN Redirects
RewriteEngine On
# 301 redirect to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 301 redirect to https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# END Redirects

Other ways to redirect?

Some people complained my examples used too many redirects. In that case, you’re welcome to research on your own and try other methods of redirection.

Read below and see which one you like best (beware, they all have their implications):

  • https://simonecarletti.com/blog/2016/08/redirect-domain-http-https-www-apache/
  • https://www.codexworld.com/redirect-non-www-to-www-http-to-https-using-htaccess-file/
  • https://www.digitalocean.com/community/questions/redirect-http-https-www-to-https-non-www
  • https://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www
  • https://serverfault.com/questions/523199/redirect-all-http-and-https-non-www-urls-to-https-www-example-com-via-htaccess
  • NGINX? – try this.
  • And in case you need to do regular 301 redirects for new page or address

Share this post:

Share on FacebookShare on X (Twitter)Share on LinkedInShare on WhatsAppShare on EmailShare on SMS

Read all my posts on WordPress hosting

About Johnny

Right on the edge of WordPress development! 10+ years of WordPress design, development, hosting, speed optimization, product advisor, marketing, monetization. I do all that.

More WordPress Guides

Attracting TALENTED Developers to Your Project

How to Set a Sales Meeting

DISRUPTING a WordPress plugin market

The Best Copywriting is probably NATURAL

Should you load Google Analytics in HEADER or FOOTER?

Restrict Content Pro – WordPress membership plugin review 2020

Reader Interactions

19 Comments

  1. Mike

    May 7, 2019 at 11:50 am

    hi, method 1 not working me at all.

    Reply
    • Johnny

      May 7, 2019 at 12:29 pm

      Then try method 2?

      Reply
  2. Barry

    August 10, 2019 at 10:03 pm

    Method 2 works for me (non-WordPress site). Now, it doesn’t matter if the request is http or https, or if it contains www, or not. All combinations of those work for my desired result: non-www and https.
    Thank you!

    Reply
  3. Kris

    August 16, 2019 at 12:13 am

    The problem with Method #2
    it creates a redirect chain, with 2 redirects
    for seo reasons, all should be done with one.

    Reply
    • Johnny

      August 17, 2019 at 10:53 am

      Method #2 is there because it works and the redirect chain is really not even an issue. Even if you’re worried about SEO, you have to know that Google will index the right one anyway. The redirect is only there for those who enter the wrong version.

      Back in the days, many people did it like that and I still see it used today. It’s offered as an alternate in case method #1 doesn’t work for whatever reason. 🙂

      Reply
  4. Avinash

    November 27, 2019 at 11:54 pm

    can i use this code for blogger please reply

    Reply
    • Johnny

      November 29, 2019 at 1:52 am

      What do you mean? Is that another CMS?

      Reply
  5. Hoasiso

    December 9, 2019 at 5:46 am

    I use nginx server how do i use 301 Redirects to HTTPS (with and without WWW) .thanks

    Reply
    • Johnny

      December 9, 2019 at 11:39 am

      Hi Hoasiso,

      Please try this: https://bjornjohansen.no/redirect-to-https-with-nginx

      Reply
  6. brandbay

    December 17, 2019 at 11:41 pm

    Hi,

    Is there a way to redirect direct from non www http to www https?
    I would like to have straight redirect -> https://domain.pl to https://www.domain.pl and skip redirect chain: https:// to https:// to https://www.

    Reply
    • Johnny

      December 18, 2019 at 12:03 pm

      METHOD #2 will do what you ask, but the reason why it isn’t method #1 for me is because it sometimes doesn’t work on some servers/environments. With that said, method #1 is safe and fast enough. Any redirect chain will only cost you 100-200ms the very first visit and then nothing after that. If you want the fastest redirect ever, use Cloudflare page rule. But really, you should be fine with method 1.

      Reply
      • brandbay

        December 19, 2019 at 5:59 am

        Really thanks for your explanation!

        Reply
  7. Roberto Porcar

    May 15, 2020 at 3:42 am

    I dont remember how many times I back to visit this article.

    Then… finally I write my name here.

    I WAS HERE. ^_^

    Great stuff as always.

    Reply
    • Johnny

      May 15, 2020 at 9:41 am

      Lol, yeah. It’s written for me as reference, too. That’s why I linked to it from homepage. Hahaha…thank for signing this mountain guestbook. 😀

      Reply
  8. Patrick

    October 29, 2020 at 7:51 am

    We recommend Redirection WP plugin, a few clicks and it’s all redirected nicely

    Reply
  9. Gabor

    February 26, 2021 at 8:49 am

    Hi! I use this code on swift and it works with www and non www. Could you check if this is slower than your method 1 or 2?

    # Redirect non-www to (ssl) www
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^barangolunk\.hu [NC]
    RewriteRule ^(.*)$ https://www.barangolunk.hu/$1 [L,R=301]
    # Redirect non-SSL to SSL
    RewriteCond %{HTTPS} !on
    RewriteCond %{SERVER_PORT} !^443$
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

    Reply
    • Johnny

      February 26, 2021 at 1:54 pm

      I’m not a regex expert. But if you really care, then test and see for yourself.

      Reply

Leave a Reply to Johnny Cancel reply

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

Footer

More links

  • Gadget reviews
  • Try my free WPJ plugins
  • Join the WPJ FB Group
  • WPJ YouTube & newsletter
  • Become a WPJ Affiliate

Popular Reviews

  • Best WordPress Hosting
  • Best WordPress Themes
  • Best WordPress Plugins
  • Best WordPress Cache Plugins

Services

  • Speed optimization
  • Speed optimization courses
  • WordPress hosting
  • Hire me or other experts
  • Client login

About Johnny

10+ years of WordPress design, development, hosting, speed optimization, marketing.
Contact me.

newsletter block

Copyright 2025 | WordPress guides by Johnny Nguyen

Click to Copy