【问题标题】:rewrite rule for folder to different host将文件夹的规则重写到不同的主机
【发布时间】:2020-02-21 11:34:16
【问题描述】:

有 2 个应用程序在本地网络上运行:

主机头: http://ui.local, http://api.local

在另一台服务器上,已经为 URL 重写设置了一个网站。网站的主机头是http://externalui.mydomain.com

这是 web.config 的内容:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://ui.local/{R:1}" />
                </rule>
            </rules>
        </rewrite>
<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
      </customHeaders>
    </httpProtocol>
    </system.webServer>
</configuration>

此 url 重写规则运行没有问题,并将所有对 http://externalui.mydomain.com 的 URL 重写为转到 http://ui.local

但是我想写另一条规则,所以对http://externalui.mydomain.com/api/....的请求将被转发到http://api.local/api/...。

如何为这种情况编写规则?

【问题讨论】:

  • 在此规则之上添加一个新规则,在其中添加一个条件来检查 URL 是否以 api 开头。

标签: iis url-rewriting arr


【解决方案1】:

您可以使用以下规则:

 <rule name="redirect api" stopProcessing="true">
         <match url="api/(.*)" />
         <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
         <action type="Redirect" url="http://api.local/api/{R:1}" />
 </rule>
 <rule name="all redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
          </conditions>
          <action type="Redirect" url="http://ui.local/{R:1}" />
  </rule>

注意:确保 API 规则应该是第一条规则。

【讨论】:

    猜你喜欢
    • 2012-11-20
    • 2012-12-13
    • 1970-01-01
    • 2023-03-13
    • 2016-08-16
    • 2023-03-26
    • 2012-02-24
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多