IIS重写规则在实时环境中不起作用

Haz*_*nko 8 regex iis url-rewriting azure

我有8台服务器,3台负载均衡,第4台仅用于CMS.

已为主网站添加SSL证书,但不为CMS所在的sobdomain添加SSL证书.

我写了一条规则,应该找到任何不包含"后台"的网址,并匹配任何其他网页以将其更改为https.

这适用于regexr.com但由于某种原因不起作用

<rewrite>
        <rules>
            <rule name="http to https" stopProcessing="true">
                <match url="(https?:\/\/(?!backoffice).*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                </conditions>
                <action type="Redirect" url="https://www.WEBSITENAME.com{R:1}" />
            </rule>
        </rules>
    </rewrite>
Run Code Online (Sandbox Code Playgroud)

所有4台服务器上都安装了Url Rewriting 2.1,我已经为https创建了天蓝色的负载平衡.

手动转到https工作正常(与负载平衡).

附加信息:

我已经尝试了很多规则,包括现有的答案.我可以看到发生的事情,比如将资产作为https引入,但页面本身不会重定向.

有2个负载均衡集,一个用于端口80,另一个用于端口443.我不知道这是否是核心,或者可能是重定向未发生的潜在原因.

Vic*_*yev 5

你的规则应该是这样的:

<rule name="http to https" stopProcessing="true">
    <match url=".*" />
    <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{REQUEST_URI}" pattern="/backoffice" negate="true" />
    </conditions>
    <action type="Redirect" url="https://www.WEBSITENAME.com{R:0}" />
</rule>
Run Code Online (Sandbox Code Playgroud)

此规则将使用/backoffice路径排除请求.

另外,对于混合内容的问题,您需要修复css/js/images到亲戚的路径.例:

<img src="/path/to/your/image.jpg"/>
Run Code Online (Sandbox Code Playgroud)

修复混合内容的另一种方法是创建出站规则,它将更改输出HTML(替换http:https:):

<rewrite>

  ...

  <outboundRules>
    <rule name="Rewrite external references to use HTTPS" preCondition="IsHTML">
      <match filterByTags="Script, Link, Img, CustomTags" customTags="HTML5Tags" pattern="^http://(.*)$" />
      <action type="Rewrite" value="https://{R:1}" />
    </rule>
    <preConditions>
      <preCondition name="IsHTML">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
      </preCondition>
    </preConditions>
    <customTags>
      <tags name="HTML5Tags">
        <tag name="Video" attribute="src" />
      </tags>
    </customTags>
  </outboundRules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)


Haz*_*nko 5

使用前面的答案作为起点,我做了一些小的改动,使用HTTP_HOST而不是REQUEST_URI进行模式否定,它可以工作.

<system.webServer>
    <rewrite xdt:Transform="InsertIfMissing">
        <rules>
            <rule name="http to https" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$" />
                    <add input="{HTTP_HOST}" pattern="^backoffice\.WEBSITENAME\.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="https://www.WEBSITENAME.com/{R:0}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)