【问题标题】:Web.config : force HTTPS and WWW with VARIABLE domain nameWeb.config : 使用 VARIABLE 域名强制 HTTPS 和 WWW
【发布时间】:2021-04-12 03:23:31
【问题描述】:

我已经看到了很多接近的答案,比如这个:IIS Redirect non-www to www AND http to https,但它与我的情况并不完全匹配。

我有一个网站,在其主域名之上有 2 个域别名。为每个版本显示正确的内容是在代码本身中完成的。

我尝试使用 WEB.CONFIG 文件在 2 种情况下重写:不使用 HTTPS 和/或 URL 开头没有 WWW 时。

所以http://example.comhttps://example.comhttp://www.example.com 都将被重写为https://www.example.com 如果使用其域别名之一访问该站点,同样的情况,例如http://examplealias.com 将变为https://www.examplealias.com

这个答案

<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
      <add input="{HTTP_HOST}" pattern="^[^www]" negate="true" />
      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  </conditions>
  <action type="Redirect" url="https://www.example.com/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>

对我不起作用,因为我无法对域名进行硬编码。

我试过了

<rule name="Force WWW and SSL" enabled="true" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAny">
      <add input="{HTTP_HOST}" pattern="^[^www]" negate="true" />
      <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  </conditions>
  <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>

但是如果我打电话给http://www.example.com,这会导致https://www.www.example.com

结合这两个条件的正确方法是什么?

【问题讨论】:

标签: iis web-config


【解决方案1】:

好吧,显然在我的情况下,最好不要结合规则:

    <rule name="Redirect naked domains (that do NOT have a custom sub-domain) to www.domain.com" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll">
            <add input="{HTTP_HOST}" pattern="^www." negate="true" />
        </conditions>
        <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    <rule name="Redirect non-https urls to https" enabled="true" stopProcessing="false">
        <match url="(.*)" />
        <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>

这很好用。

【讨论】:

    猜你喜欢
    • 2014-01-20
    • 2017-03-30
    • 2017-07-13
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 2023-03-23
    相关资源
    最近更新 更多