首页 > 解决方案 > RewriteRules 将特定域 https 重定向到 http,其他域相反

问题描述

我有一个域名为 example.info 的网站。最近我获得了两个新域:example.com 和 example.org。

我在 example.info 上有一个 SSL 证书,我将所有 http 重定向到 https,如下所示:

  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

现在我希望 example.com 成为主要 url,而另外两个我只想重定向到 example.com。

我将 SSL 证书从 .info 更改为 .com,一切都很好,但我的问题是,例如 Google 将我从https://example.info链接到现在它失败了,因为该 url 没有 SSL证书了。所以我想做的是将https://example.info重定向到https://example.com

我尝试了不同的方法,但这是不起作用的解决方案之一。

  RewriteCond %{HTTPS} off
  RewriteCond %{REQUEST_URI} !^(www\.)?example\.info$
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 
  RewriteCond %{HTTPS} on
  RewriteCond %{HTTP_HOST} ^(www\.)?example\.info$
  RewriteRule ^ https://example\.com [L,R=301]

我究竟做错了什么?如何实现将 https://example.info 重定向https://example.com保持重定向到 https for example.com?

//// 编辑

如果我因为没有有效证书而无法重定向https://example.info,如何防止在编写http://example.info时将其重定向到 https?

标签: .htaccessmod-rewrite

解决方案


我认为你能做的最好的就是:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.(info|org)$
RewriteRule ^ https://example\.com%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

推荐阅读