最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

htaccess https redirect from www to non-www

programmeradmin0浏览0评论

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:

     → 
 → 

But when I visit my domain as or , 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?

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     → http://example
http://www.example/abc → http://example/abc

But when I visit my domain as https://www.example or https://www.example/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?

Share Improve this question edited Feb 11, 2017 at 5:01 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Feb 11, 2017 at 1:52 DinhTvDinhTv 771 gold badge3 silver badges11 bronze badges 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 2 - i want reditect: whent visitors https://www.example reditect to https://example 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
Add a comment  | 

1 Answer 1

Reset to default 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\$
    RewriteRule ^(.*)$          "https://example/$1" [R=301,L]

    # remaining htaccess mod_rewrite CODE for WordPress
</IfModule>

This will do the following example redirects:

http://www.example/     → https://example/
http://www.example/abc  → https://example/abc
https://www.example/    → https://example/
https://www.example/abc → https://example/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/abc
http://sub-dmoani.example/abc  → https://example/abc
http://www.other-domain/abc    → https://example/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\$
    RewriteRule ^(.*)$       "%{ENV:proto}://example/$1" [R=301,L]

    # remaining htaccess mod_rewrite CODE for WordPress
</IfModule>

This will do the following example redirects:

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

https://www.example/    → https://example/
https://www.example/abc → https://example/abc
发布评论

评论列表(0)

  1. 暂无评论