我看过很多关于重写的文章。都好。但没有一个涵盖这种具体情况。所以这是我的问题:希望你能帮忙。因为我无法让它发挥作用。
- 我们在domain.com(非www)上运行网站
- 我们已设置 ssl(因此仅限 https)
- 我们有 1 个非 www 版本(非 www 版本)的证书
当我们执行所有四个测试用例时,3 个正常,1 个不正常
- http://domain.com =>https://domain.com =>
- http://www.domain.com =>https://domain.com => 好的
- https://domain.com => 好的
- https://www.domain.com => 错误。证书警告不安全
问题:现在为什么数字是 4 https://www给我这个错误。我希望第一条规则能够接收并将我们发送到非 www 版本。我该如何解决这个问题?
感谢任何帮助;P肖恩
<小时/>这是我当前的 htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect www to non-www first
# Added based on article http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
# Then redirect http to https (if necessary)
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>
最佳答案
您可以在 .htaccess 中使用它:
<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect www to non-www first
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
# Then redirect http to https (if necessary)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
</IfModule>
但是对于证书错误您无能为力。
阅读:Redirecting https://www to https://non-www - without seeing certificate error, possible?
关于.htaccess - 重定向http=>https(非www)+ www https=>非www https(www 无证书),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28362743/