Redirect rules SSL certificate

Redirect non www to www or www to non www domains in .htaccess

Why is there a double redirect in my site?

WordPress internally redirects your site to the primary domain, which is entered in settings/general. For example, when your site url is http://www.domain.com, and you type in http://domain.com, WordPress redirects to the www domain.

Really Simple SSL adds another redirect: from https to http. To be compatible with multisite, the redirect is by default to the https variant of the request url. So if you type in http://domain.com, Really Simple SSL will redirect to https://domain.com. WordPress will then redirect to the www domain. Some people think that this is bad for SEO and site speed.

Please note that these redirects are only needed when the url is requested over the non-primary domain. As all redirects are done with 301 redirects, search results will only show the primary domain, and your users will never experience a redirect. Only old links on the web can cause a redirect.

Another issue with redirects: no SSL on www, or on non-www

You might also have SSL only for the www domain, or only for the non-www domain. If that is the case, you’ll find that your non SSL domain won’t function anymore if you move to ssl. To counter this, you could redirect all  requests coming in on the non www domain to the www domain.

How to bypass these redirects

You can also edit the .htaccess manually, by adding these lines:

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

These rules in human language:

When rewrite condition “all domains that not start with www” is met
Redirect to the www domain.

If you want www redirecting to non www, add this to your .htaccess:

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

The end result in your .htaccess should look something like this, where the new redirect is added separately above the redirect inserted by Really Simple SSL:

#redirect all www requests to non-www.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
</IfModule>

#redirect all non https requests to SSL
# BEGIN rlrssslReallySimpleSSL rsssl_version[2.5.11]
<IfModule mod_rewrite.c> RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL

Related Articles

  • www not redirecting to non-www on cloudflare

    Recently I encountered an interesting issue with redirection from www to non-www. The non www site worked perfectly after migration to SSL, but the www site gave a 404 not...
  • Disabling ssl for one page only

    The problem: services that don’t support SSL I think the best way to go is to get your entire site on SSL. But sometimes there are services that are not...
  • Website not accessible with SSL

    If you cannot access your website, but do not have redirect loop errors, your SSL certificate might be invalid. Normally Really Simple SSL checks if ssl is available, and only...
  • Does SSL improve my security?

    Sure. But I would not say it is the first thing should do. To prevent your site from getting hacked, start with the following: Do not use Admin as your...

15 Comments

  • Jorge Martinez

    Hi Rogier,

    Is there a specific place in the .htaccess where we should put this code to make it work correctly? I want to make sure that is anyone types http://www.certificacione.com or http://www.certificacione.com it gets redirected automatically to https://certificacione.com as my SSL doesn’t work with the www (gives a security warning in the browser). I try inserting your code as follows but I dont see the redirection from www to non www happening. Let me know if I place it in the wrong place or with the wrong format.

    # BEGIN rlrssslReallySimpleSSL rsssl_version[2.3.14]

    RewriteEngine on
    RewriteCond %{HTTPS} !=on [NC]
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # END rlrssslReallySimpleSSL

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

  • Rogier Lankhorst

    Could you try the following:
    Add it to the top, and add “rewrite engine on” before, see example below. Let me know if that helps!

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

    # BEGIN rlrssslReallySimpleSSL rsssl_version[2.3.14]
    RewriteEngine on
    RewriteCond %{HTTPS} !=on [NC]
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    # END rlrssslReallySimpleSSL
    # BEGIN WordPress
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress

  • Anton

    Hello!
    Sorry to disturbe you, but I have problems with redirect from www to non www.

    http://mysite.com > https://… works great
    But http://www.mysite.com and https://www.mysite.com do not works to redirect to https://mysite.com

    Sorry for my bad english. Code from my htaccess is here:

    # BEGIN rlrssslReallySimpleSSL rsssl_version[2.4.3]

    RewriteEngine on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # END rlrssslReallySimpleSSL
    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPress

  • Rogier Lankhorst

    You can add the code from this article to the top of your .htaccess to redirect from www to non-www.

  • Anton

    I’ve some as you showed in article but that doesn’t work

  • Rogier Lankhorst

    An alternative you could try is this:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

      • Rogier Lankhorst

        Did you put that www to non www redirect at the top of your .htaccess?

          • Rogier Lankhorst

            No idea why it doesn’t work for you. You could try to make it more general.

            RewriteCond %{HTTP_HOST} ^www\. [NC]
            RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

            I’m out of ideas here 🙁

            The online redirect checker also shows a 302 redirect, but that is not in the .htaccess. Where does that come from? Do you have a server setting redirecting as well?
            http://www.redirect-checker.org/index.php

  • giorgos stavropoulos

    hallo i have a problem!
    i have managed to put
    #redirect all www requests to non-www.

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

    and everything is ok with https look but when i add the rest code i camnnot enter the site! can you help?

    #redirect all non https requests to SSL
    # BEGIN rlrssslReallySimpleSSL rsssl_version[2.5.11]
    RewriteEngine on
    RewriteCond %{HTTPS} !=on [NC]
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    i place it right after the first code

    thanks for your time

    • Mark Wolters

      Hi Giorgos,

      it would be easier to enable the ‘htaccess redirect’ option in the Really Simple SSL options because that will choose the best solution for you automatically and you don’t have to worry about editing the file yourself :). If that didn’t work and you need to edit the .htaccess and now your site is not working anymore, could you explain what error message you are getting exactly?

      Mark

  • Rogier Lankhorst

    You can follow the steps in this article, or you can use Really Simple SSL pro, which handles this as well if you enable .htaccess redirect.

Leave a Comment