首页 > 解决方案 > IIS URL 重写一些覆盖其他规则的规则

问题描述

我们正在将我们的主站点分解为微服务。最初,当我们拆分站点的一部分时,它将在我们的原始服务器上成为它自己的站点,直到它可以移动到它自己的容器中。

该站点当前是一个 Angular 站点,因此您会看到我们有规则将所有内容都重写到 index.html。我正在尝试添加一个额外的规则,所以如果它找到了 url。www.domainname.com/api/auth/.*localhost:8001/{R:1}

我的理解是它将从 /auth/ 之后的 url 中获取所有内容并将其更改为 localhost:8001/{stuff after}

发生的事情是它遵循将所有内容指向索引文件的规则,并且似乎忽略了我的新规则。我第一次也是最后一次尝试过,所以我假设我的规则一定有问题。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Api Rule">
                    <match url="domainname.com/api/auth/.*" />
                    <action type="Rewrite" url="localhost:8001/{R:1}" appendQueryString="false" />
                </rule>
                <rule name="AngularJS Routes" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />   
                    </conditions>
                    <action type="Rewrite" url="/" />
                </rule>
                <rule name="CanonicalHostNameRule1">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.domainname\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.domainname.com/{R:1}" />
                </rule>
                <rule name="Redirect to https" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

标签: angulariisurl-rewritingmicroservices

解决方案


规则的评估顺序与它们指定的顺序相同。(来自文档:URL 重写模块配置参考 - 规则评估

元素的url属性match与 URL 的 PATH 部分一起使用。您不应指定域。

<rule name="Api Rule">
    <match url="^api/auth/(.*)" />
    <action type="Rewrite" url="localhost:8001/{R:1}" appendQueryString="false" />
</rule>

如果要与域匹配,可以添加条件

<rule name="Api Rule">
    <match url="^api/auth/(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^domain\.com$" />
    </conditions>
    <action type="Rewrite" url="localhost:8001/{R:1}" appendQueryString="false" />
</rule>

推荐阅读