【问题标题】:Redirecting http=>https (non-www) + www https=> non-www https (no certificate for www)重定向http=>https(非www)+www https=>非www https(没有www证书)
【发布时间】:2015-04-06 09:59:57
【问题描述】:

我看过很多关于重写的文章。都好。但没有人涵盖这种确切的情况。所以这是我的问题:希望你能提供帮助。因为我无法让它工作。

  • 我们在 domain.com 上运行网站(非 www)
  • 我们设置了 ssl(因此仅限 https)
  • 我们有 1 个非 www 版本(不是 www 版本)的证书

当我们执行所有四个测试用例时,3 个正常,1 个不正常

  1. http://domain.com => https://domain.com =>
  2. http://www.domain.com => https://domain.com => 好的
  3. https://domain.com => 好的
  4. 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>

【问题讨论】:

  • 您需要通配符证书,但我会在这里为您提供帮助。尝试删除 &lt;IfModule mod_rewrite.c&gt;&lt;/IfModule&gt; 并告诉我是否收到任何错误 500。
  • 问题是这已经是一个多域证书,所以这意味着我必须添加每个 www。所有域的版本分别?我不知道任何通配符多域证书
  • 通配符证书是一种特殊类型,其中主机的名称是*.example.com,而不是example.com。这允许您将证书用于 ALL 您的子域。 (它不会适用于 another.cool.example.com 之类的子域。为此,您需要一个通配符证书 per 子域。)www 可能被视为子域(它通常映射到文件夹www,可能有/是指向public_html 的链接)。
  • 试试这个而不是最后一部分:RewriteCond %{HTTPS} =off RewriteCond %{HTTP_HOST} ^(.*) RewriteRule ^/?(.*) https://%1/$1 [R=301,L](按行拆分,我不能在评论中这样做)。
  • 其实第一次检查应该是RewriteCond %{HTTPS} ^off$。我已经在htaccess.madewithlove.be 上对其进行了测试,它似乎可以工作。

标签: .htaccess mod-rewrite


【解决方案1】:

您可以在 .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?

【讨论】:

    【解决方案2】:

    这是仅通过 htaccess 或 nginx conf 无法解决的。

    需要一个证书来涵盖域的 www 和非 www 版本

    因此,通配符证书或多域证书,其中 www 和非 www 都包含在单独的域中

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      相关资源
      最近更新 更多