代码之家  ›  专栏  ›  技术社区  ›  Adam

IIS重写规则将非www重定向到动态域等效且始终为https

  •  3
  • Adam  · 技术社区  · 8 年前

    我想要的是所有非https或没有 www 预设的被重定向到: "https://www." + domain name + possible query string parameters .

    我有一条重写规则( found here ):

    <rule name="non-www to www https" enabled="true" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^[^\.]+\.[^\.]+$" />
        <add input="{HTTPS}" pattern="on" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" />
    </rule>
    

    但是,当在浏览器地址栏中键入以下域时,不会发生重定向(由于我没有通配符DNS SSL证书,因此我收到了一个安全证书错误):
    https://example.com/
    http://example.com/

    但是 example.com (无协议),正确重定向到 https://www.example.com/

    还要注意,在上面的规则中,我正在动态匹配主机名,而不仅仅是在“example.com”上,因为我希望这个规则适用于多个域名。

    然后我也检查了这个 post ,它有一个简洁的规则:

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

    我认为这正是我想要的,但是我如何使本例中的域名动态并在重定向中保留它(就像第一个代码示例所做的那样)?(原始海报在过去6个月内没有登录,因此我在这里提问)

    此外,我还检查了 this post ,这似乎也是一个很好的候选人:

    <rule name="Redirect top domains with non-www to www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" />
          </conditions>
          <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
          <serverVariables>
            <set name="Redirect" value="false" />
          </serverVariables>
     </rule>
    
    
    <rule name="Force HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*stage\..*" negate="true" />
            <add input="{HTTP_HOST}" pattern=".*dev\..*" negate="true" />
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
     </rule>
    

    但是后来 http://example.com 重定向到 https://example.com 我仍然得到了安全例外。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Kul-Tigin    8 年前

    首先,我强烈建议您获得一个新的SSL证书,该证书同时支持 example.com www.example.com . 这种证书实际上是大多数SSL提供商的标准证书,它不必是通配符证书。否则,您将无法处理对的请求 https://example.com 就目前而言,我认为这是一个问题。

    你最重要的两条规则应该如下。

    附笔。 301重定向被浏览器缓存了一段时间。谷歌 clear 301 redirect cache 在测试新规则之前,为您的浏览器。

    <rule name="All HTTP to HTTPS+WWW" stopProcessing="true">
        <match url=".*" />
        <conditions trackAllCaptures="true">
            <add input="{SERVER_PORT_SECURE}" pattern="0" />
            <add input="{HTTP_HOST}" pattern="(?:localhost|stage\.|dev\.)" negate="true" />
            <!-- here with this 3rd condition we capture the host name without "www." prefix into {C:1} variable to use in redirect action -->
            <add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
        </conditions>
        <action type="Redirect" url="https://www.{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>                
    <rule name="All HTTPS With No WWW to HTTPS+WWW" stopProcessing="true">
        <match url=".*" />
        <conditions trackAllCaptures="false">
            <add input="{SERVER_PORT_SECURE}" pattern="1" />
            <add input="{HTTP_HOST}" pattern="(?:localhost|stage\.|dev\.)" negate="true" />
            <add input="{HTTP_HOST}" pattern="^www\." negate="true" />
        </conditions>
        <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>