【问题标题】:htaccess rewrite for https domainshtaccess 重写 https 域
【发布时间】:2015-10-21 10:03:50
【问题描述】:

我已将hole webstore 更改为https。所以我想将除移动子域(http://m.my-store.com)之外的所有域重写为https://www.my-store.com

#First rewrite any request to the wrong domain to use the correct one (here www.)
#mobile subdomain shouldn't rewrite
RewriteCond %{HTTP_HOST} !m\.
RewriteCond %{HTTP_HOST} !^www\.my-store\.com$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.my-store\.com$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

结果:

http://my-store.com         OK (correct rewrite to https://www.my-store.com)
http://www.my-store.com     OK (correct rewrite to https://www.my-store.com)
https://my-store.com        X (stays with https://my-store.com)
https://www.my-store.com    OK (correct rewrite to https://www.my-store.com)
http://m.my-store.com       OK (correct rewrite to https://www.my-store.com)

【问题讨论】:

  • http://m.my-store.com OK (correct rewrite to https://www.my-store.com)
  • 抱歉,复制和粘贴错误。应该是m.my-store.com OK(留在m.my-store.com
  • https://my-store.com 保持相同的 URL,这不是你想要的吗?
  • 不,我想将 URL (https://my-store.com) 重定向到 URL (https://www.my-store.com)
  • 由于您只有 %{HTTP_HOST} 作为目标,它不会在您的第三个示例中将 www 添加到 URL。您可能需要创建两组来添加 https(一组用于 m.my-store.com,一组用于其他)

标签: apache .htaccess mod-rewrite redirect https


【解决方案1】:

如果www 不在请求中,我会做的只是检查域而不是尝试匹配。相反,只需检查基本域,这意味着没有 www,所以重定向。 您还可以利用 [OR] 和这一规则,它会处理所有情况。

你在下面这条规则。

RewriteCond %{HTTP_HOST} ^my-store\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^(.*)$ https://www.my-store.com/$1 [L,R=301]

下面不要这样。

我在上面的重写规则中使用了 actual 域,这样您就不会遇到变量问题,因为您将使用 OR 这个方法。意思是如果你有这样的规则。

RewriteCond %{HTTP_HOST} ^my-store\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

你的请求http://www.my-store.com 你最终会得到https://www.www.my-store.com 所以使用我提供的第一条规则。

您的结果将使用上面的第一条规则。

http://my-store.com         (rewrites to https://www.my-store.com)
http://www.my-store.com     (rewrites to https://www.my-store.com)
https://my-store.com        (rewrites to https://www.my-store.com)
https://www.my-store.com    (does nothing - https://www.my-store.com)
http://m.my-store.com       (does nothing - http://m.my-store.com)

【讨论】:

  • 我使用了您的规则RewriteCond %{HTTP_HOST} ^my-store\.com$ [OR] RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} !^m\. RewriteRule ^(.*)$ https://www.my-store.com/$1 [L,R=301],但结果与我的规则相同。您的变体更好,因为它的行数更少;)但是https://my-store.com 没有重定向到https://www.my-store.com
  • https://my-store.com (rewrites to https://www.my-store.com) 不起作用:(
  • 如果你使用我的第一条规则,它确实如此。清除浏览器缓存,然后重试。
  • 或者在其他浏览器中尝试确保没有缓存
  • 好吧,和我提供的规则无关。它不起作用,因为您的证书无效。如果证书首先无效,则无法重定向。请参阅此答案以解释为什么它对您不起作用。您需要koeder-laden.de stackoverflow.com/questions/33039666/… 的有效证书
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-31
  • 2011-07-23
  • 1970-01-01
  • 2019-06-12
  • 2012-08-12
相关资源
最近更新 更多