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

点点圈 提交于 2019-12-01 23:25:45

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.

Rizwan Ranjha

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!