【问题标题】:IIS rewrite rule not working in live environmentIIS 重写规则在实时环境中不起作用
【发布时间】:2017-09-13 21:43:17
【问题描述】:

我有 4 台服务器在 azure 中,3 台是负载平衡的,第 4 台仅用于 CMS 目的。

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

我写了一个规则,应该找到任何不包含“后台”的 url 并匹配任何其他页面以将其更改为 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>

Url Rewriting 2.1 安装在所有 4 台服务器上,并且我在 azure 中为 https 创建了负载平衡。

手动转到 https 可以正常工作(以及负载平衡)。

附加信息:

我尝试了许多规则,包括现有的答案。我可以看到正在发生的事情,例如以 https 的形式引入的资产,但页面本身不会重定向。

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

【问题讨论】:

  • 如果你用&lt;match url="(.*)" /&gt;替换你的正则表达式会发生什么。这行得通吗?
  • 重定向仍然没有发生。此外,css/js/imagery 不会加载建议混合 http/https

标签: regex azure iis url-rewriting


【解决方案1】:

你的规则应该是这样的:

<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>

此规则将排除具有/backoffice 路径的请求。

此外,对于混合内容的问题,您需要将 css/js/images 的路径修复为亲属。示例:

<img src="/path/to/your/image.jpg"/>

另一种修复混合内容的方法是创建出站规则,这将更改您的输出 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>

【讨论】:

  • 后台是一个子域而不是一个文件夹,但我会试一试
  • 否定适用于后台。但我在客户端得到了奇怪的结果。图像都无法加载 301。这是因为页面本身实际上并未重定向到 https。
  • 一周后回到办公室...只是想也许否定甚至不起作用。因为它根本不重定向页面。我可以看到所有带有 301 的图像都说他们已经转移到 https,但是他们没有重定向,页面也没有重定向。
【解决方案2】:

使用前面的答案作为起点,我做了一些小改动,使用 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>

【讨论】:

  • 我认为维克多应该得到奖励,因为他不知道这个细节:)
  • 当然正确,他也确实帮助我得出了结论。
猜你喜欢
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 2015-07-10
  • 2017-03-08
  • 2020-01-18
相关资源
最近更新 更多