regex - IIS 重写规则在实际环境中不起作用

标签 regex azure iis url-rewriting

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

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

我编写了一条规则,该规则应该找到任何不包含“backoffice”的 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 效果很好(与负载平衡一起)。

其他信息:

我尝试了很多规则,包括现有的答案。我可以看到正在发生的事情,比如 Assets 被作为 https 引入,但页面本身不会重定向。

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

最佳答案

你的规则应该是这样的:

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

关于regex - IIS 重写规则在实际环境中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058263/

相关文章:

python - python中编译的正则表达式对象的类型

iis - 无法在 Windows 10 预览版的 Microsoft Edge(Project Spartan)中打开 localhost

c# - MVC 4 - JSON ajax 操作结果的 GZIP 压缩

iis - 使用 IIS 的 Dropbox 内的本地站点

python - 使用 python/psycopg2 循环遍历 PostgreSQL 表时发出 w/UPDATE

javascript - 正则表达式 JavaScript If/Else 语句

regex - 正则表达式帮助

java - 为什么我的 @Scheduled Spring Boot 任务在 Azure 中运行不一致?

azure - 无法使用 SQL 身份验证连接到 Azure SQL Server

c# - 在 Azure VM (virtocommerce) 上找不到 LibSass.x64