IIS重定向到HTTPS,不同的规则相同的结果

Mir*_*ror 1 iis url-rewriting url-rewrite-module

在尝试将 http 流量重定向到 https 时,我发现了以下两个规则,它们似乎在做同样的事情,但是它们在两个地方有细微的差异。我应该选择其中一个而不是另一个吗?有什么好处吗?(性能、极端情况等)

规则1:

<rule name="HTTP to HTTPS Redirect" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
Run Code Online (Sandbox Code Playgroud)

规则 2:

<rule name="HTTP to HTTPS Redirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
        redirectType="Permanent" />
</rule>
Run Code Online (Sandbox Code Playgroud)

差异在于输入和重定向 URL,其中一个使用 {R:1},另一个使用 REQUEST_URI。

先感谢您

Kul*_*gin 5

两条规则给出相同的结果。而且它们之间的性能没有显着差异。此外,IIS 默认在内核级别缓存此类规则。这意味着请求很可能会从 HTTP 内核模式驱动程序响应,而不会到达 Web 应用程序。因此,这些规则将在您无法衡量差异的情况下发挥作用。

然而,如果您喜欢进行不必要的优化(就像我有时做的那样:$),请检查以下规则。

<rule name="HTTP to HTTPS Redirect" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions>
        <add input="{SERVER_PORT_SECURE}" pattern="0" ignoreCase="false" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
Run Code Online (Sandbox Code Playgroud)

以下是对该规则的不必要的改进;

  • 通配符匹配比正则表达式便宜。
  • 区分大小写的又名二进制比较 ( ignoreCase="false") 更便宜。
  • 寻找0比寻找{SERVER_PORT_SECURE}便宜。off{HTTPS}