【问题标题】:Rewrite rules for with www and without www重写有 www 和没有 www 的规则
【发布时间】:2014-03-03 23:59:04
【问题描述】:

我已经搜索了很多,但我找不到我的具体问题的答案。

目前,如果所有用户键入以下内容之一,他们都会重定向到 https://www.mydomain.com

  • http://mydomain.com(将http替换为https并在此处添加www)
  • http://www.mydomain.com(此处将 http 替换为 https)
  • https://mydomain.com(在此处附加 www)

我的 .htaccess 中有此代码用于上述条件。

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

我不知道如何停止任何子域的重定向。例如如果键入以下内容,则不应将用户重定向到 https

  • abc.mydomain.com(子域可以是任何名称)
  • http://abc.mydomain.com

知道怎么做吗?

提前感谢您的帮助。

【问题讨论】:

标签: regex apache .htaccess mod-rewrite rewrite


【解决方案1】:

这条规则应该有效:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

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

【讨论】:

  • 嘿,Anubhava,它没有用。我收到循环重定向错误。
  • Anubhava,您的代码仅适用于 www,不适用于 www。但它仍然没有停止子域的重定向。 abc.mydomain.com 仍然重定向到 abc.mydomain.com。我不希望我的子域重定向到 SSL。
  • 是的,我犯了一个错误,现在修复它。再试一次
【解决方案2】:

尝试:

RewriteEngine On

RewriteCond %{SERVER_PORT} 80

RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] 

kj

【讨论】:

  • 它不适用于子域。当我输入 abc.mydomain.com 时。它重定向到mydomain.com
猜你喜欢
  • 2020-11-28
  • 2014-08-23
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 2015-02-11
  • 2017-05-09
  • 2023-03-14
  • 2011-09-18
相关资源
最近更新 更多