Search code examples
iisurl-rewritingarr

rewrite rule for folder to different host


There are 2 applications running on local network:

host headers: http://ui.local, http://api.local

On another server, a website is already setup for url rewriting. Host header of the site is http://externalui.mydomain.com

Here is web.config contents:

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

This url rewriting rule is running without a problem and rewrites all URLs made to http://externalui.mydomain.com to go to http://ui.local

However i want to write another rule so requests made to http://externalui.mydomain.com/api/.... will be forwarded to http://api.local/api/... instead.

How to write a rule for this condition?


Solution

  • You could use the below rule:

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

    Note: make sure the API rule should be the first rule.