IIS URL 重写不起作用 - 404 错误

Lyd*_*don 5 .net iis url-rewriting

我正在使用 IIS7 中的 URL 重写功能。我在端口 80 上设置了一个带有一些 URL 重写规则的网站。

第一个规则需要指向端口 8090 上的 Web 应用程序,另一个规则需要指向端口 8091 上的 Web 应用程序。

需要配置规则以便:

  1. http://localhost/重写为http://localhost:8090
  2. http://localhost/test重写为http://localhost:8091.

这是我正在使用的规则:

<system.webServer>
    <rewrite>
        <rules>
            <clear />
            <rule name="Site2" enabled="true" stopProcessing="true">
                <match url="^.*/test/.*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <action type="Rewrite" url="http://{HTTP_HOST}:8091/{R:0}" />
            </rule>
            <rule name="Site1" enabled="true" stopProcessing="true">
                <match url=".*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <action type="Rewrite" url="http://{HTTP_HOST}:8090/{R:0}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

我会提到“Site1”规则正在发挥作用。如果我转到http://localhost/,我会看到托管在端口 8090 上的 Web 应用程序。如果我转到http://localhost/test,我会看到 404 错误。

Lyd*_*don 2

我最终确实按照要求完成了这项工作。以下是在端口 80 上托管的默认网站上使用的 web.config 文件。这使我可以浏览到http://my.domain.com/test1并获取在http://上托管的网站本地主机:8093/test1

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpRedirect enabled="false" destination="https://my.domain.com" exactDestination="true" />
        <rewrite>
            <rules>
                <clear />
                <rule name="Redirect to correct test1 address" stopProcessing="true">
                    <match url="^test1$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="http://my.domain.com/test1/" />
                </rule>
                <rule name="Redirect to correct test2 address" stopProcessing="true">
                    <match url="^test2$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="http://my.domain.com/test2/" />
                </rule>
                <rule name="Redirect to correct test3 address" stopProcessing="true">
                    <match url="^test3$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="http://my.domain.com/test3/" />
                </rule>

                <rule name="Reverse Proxy to test1" stopProcessing="true">
                    <match url="^test1/*(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="http://localhost:8093/test1/{R:1}" />
                </rule>
                <rule name="Reverse Proxy to test2" stopProcessing="true">
                    <match url="^test2/*(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="http://localhost:8093/test2/{R:1}" />
                </rule>
                <rule name="Reverse Proxy to test3" stopProcessing="true">
                    <match url="^test3/*(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="http://localhost:8093/test3/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)