Redirect rules SSL certificate

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

If you have an ssl certificate on a www domain, but not on the non www domain, you’ll find that your non www 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. A way to do this is:

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]

 

 

Related Articles

  • 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...
  • My website does a double redirect to https, or not 301 redirect

    Some users ask for a fix for a double redirect. But Really Simple SSL does not do a double redirect! The plugin uses two ways to redirect: in the .htaccess...
  • 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...
  • Manually insert .htaccess redirect http to https

    Really Simple SSL has an option which inserts the detected .htaccess redirect rules. There are several server configurations, which each require their own .htaccess redirect. The plugin tries to detect...

11 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

Leave a Comment