4

I have added .htaccess https redirect from www to non-www with:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.
    RewriteCond %{HTTPS}s ^on(s)|off
    RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
    RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
</IfModule>`

When I visit my domain, the following redirects happen without any problem:

http://www.example.com     → http://example.com
http://www.example.com/abc → http://example.com/abc

But when I visit my domain as https://www.example.com or https://www.example.com/abs, my domain does not redirect and shows the error code: SSL_ERROR_INTERNAL_ERROR_ALERT (in Firefox) or ERR_SSL_PROTOCOL_ERROR (in Chrome)

I have used WordPress and the plugin Really Simple SSL.

Any idea on how to fix my error?

3
  • Question: 1. Did you install any SSL certificate? 2. Do you want all your http requests to go to https?
    – Fayaz
    Commented Feb 11, 2017 at 2:20
  • 1- i have install SSL certificate Comodo. current website visitors as https://example.com 2 - i want reditect: whent visitors https://www.example.com reditect to https://example.com Thanks
    – DinhTv
    Commented Feb 11, 2017 at 2:31
  • I've given an answer with mod_rewrite. However, if the problem persists, then the issue is somewhere else. In that case I'll need more info. for example do you use any service like Cloudflare? What's your SSL config CODE? etc.
    – Fayaz
    Commented Feb 11, 2017 at 4:44

1 Answer 1

8

Before I give you the CODE, let me explain a few points:

Point 1:

It's better if you only allow https links. Mixing http & https for the same content breaks the security added by https. With http, you can never be sure that your visitors are shown the same page you are providing from your server.

Point 2:

Search engines consider http & https sites to be different sites, even when the domain is the same. So if not set up properly, you may get duplicate penalty. So it's recommended to go https all the way for better SEO as well.

Point 3:

Even with proper .htaccess CODE, you may get the same error if SSL is not set up properly. So after you change the .htaccess CODE, it's better to test your SSL setup against standards. You may use tool such as this.

Recommended CODE:

Based on the points above, following is the CODE you may use:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTPS}        =off   [OR]
    RewriteCond %{HTTP_HOST}    !^example\.com$
    RewriteRule ^(.*)$          "https://example.com/$1" [R=301,L]

    # remaining htaccess mod_rewrite CODE for WordPress
</IfModule>

This will do the following example redirects:

http://www.example.com/     → https://example.com/
http://www.example.com/abc  → https://example.com/abc
https://www.example.com/    → https://example.com/
https://www.example.com/abc → https://example.com/abc

# Additionally, even if your site is reachable by IP or
# some other domains or subdomains, this CODE will fix that too
http://Your_Server_IP/abc          → https://example.com/abc
http://sub-dmoani.example.com/abc  → https://example.com/abc
http://www.other-domain.com/abc    → https://example.com/abc

Not Recommended CODE:

If for some reason, you don't want to follow the above recommendation & still want to allow both http and https, you may use the following CODE (based on this answer):

<IfModule mod_rewrite.c>
    RewriteEngine On

    # set protocol variable
    RewriteCond %{HTTPS} =on
    RewriteRule ^(.*)$ - [env=proto:https]
    RewriteCond %{HTTPS} =off
    RewriteRule ^(.*)$ - [env=proto:http]

    RewriteCond %{HTTP_HOST} !^example\.com$
    RewriteRule ^(.*)$       "%{ENV:proto}://example.com/$1" [R=301,L]

    # remaining htaccess mod_rewrite CODE for WordPress
</IfModule>

This will do the following example redirects:

http://www.example.com/     → http://example.com/
http://www.example.com/abc  → http://example.com/abc

https://www.example.com/    → https://example.com/
https://www.example.com/abc → https://example.com/abc
1
  • Thx. For me, adding this to .htaccess causes no change. I'm trying to redirect https to http (we have no ssl for now) and simultaniously redirect www to non-www. Either w/ htaccess or via php in functions.php. Can you take a look at my question and see if you can halp?wordpress.stackexchange.com/questions/286464/… Commented Nov 21, 2017 at 16:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.