如何在 Opencart 中将所有 www 重定向到非 www 和 SSL Url?

How to redirect all www to non-www and SSL Url in Opencart?

我的 opencart 网站在使用 url 时遇到问题。

The problems are like - 
1. When I use http://example.com : It works fine.
2. When I use https://example.com : It works fine with my non-www ssl certificate
3. When I use http://www.example.com : I lose my styles.
4. When I use https://www.example.com : I lose my styles and works as secured page

Now I want it in these ways :
1. When I use http://example.com : It should redirect to https://example.com
2. When I use http://www.example.com : It should redirect to http://example.com so that I can't lose styles.
3. When I use www.example.com : It should redirect to https://example.com
4. When I use only example.com : It should redirect to https://example.com

这是我现在的.htaccess.txt

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L]
RewriteRule ^system/download/(.*) index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_= [L,QSA]
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

请指导我如何实现我想要的以及如何解决样式问题。 提前致谢!

试试这个(替换 .htaccess 文件的最后三行):

RewriteCond %{HTTPS} on
RewriteCond %{HTTP:X-Forwarded-SSL} on
RewriteCond %{REQUEST_URI} !^/checkout/?.*$
RewriteRule ^(.*)$ http://url.com/ [R=301,L]

并确保您的 opencart SSL 设置已打开,并设置了正确的 https:// URL。

如果上述解决方案不起作用,您可以简单地将 www 的所有调用重定向到非 www url,您的网站可以在 SSL 和样式下完美运行。

你可以试试我的解决方案。他在 2020 年工作。

# Redirect to non-www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mystore\.com [NC]
RewriteRule (.*) http://mystore.com/ [R=301,L]