WWW to NON WWW Urls (Remove WWW) using Apache (.htaccess)

后端 未结 2 501
终归单人心
终归单人心 2021-01-21 13:49

I have to redirect my website from https://www.example.com/ to https://website.com/.

SSL is properly installed on my server.

相关标签:
2条回答
  • 2021-01-21 14:14

    Please note that my SSL certificate is issued for website.com and not www.website.com.

    That is your main problem. You are never going to be able to redirect from www to non www without a valid certificate for www. The reason is the connection is handled first before the web server processes anything else including any rewrite rules. So when your browser connects to your site using the https protocol, it has to check for a valid certificate because that's the very nature of SSL to make sure the connection is secure. Then once that is done, Apache will process web server rules that you have in place like rewrites. So it can't rewrite from www to non www until the connection is completed correctly. In order for that to happen you also need a certificate for www as well. The rewrite rules are not the problem.

    This topic comes up quite frequently and there is no way around it. That's the nature of SSL/encryption and how it works.

    When buying a cert, try not buying just the domain name, because some CA's will only give you that without www. But if you use www.example.com in your CSR you will get both www.example.com and example.com in the same certificate 99% of the time. Then you won't have to worry about this issue. They are stupid cheap so it shouldn't be an issue to get another one. SSls.com has them for 4.99/yr.

    0 讨论(0)
  • 2021-01-21 14:14

    Did you Try this ?

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

    Generic htaccess redirect www to non-www

    0 讨论(0)
提交回复
热议问题