1

I am trying to write a dynamic sub-domain redirection rule using Rewrite Maps feature in IIS, given that our DNS server routes *.myapp.com to the same load balancer and ultimately the same application.

Here's my current working implementation without Rewrite Maps (simply using Redirect Rule):

<rules>
    <rule name="Redirection rule for transition convenience when client requests new sub-domain url" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^(OLD_CLIENT\.)(.*)$" />
        </conditions>
        <action type="Redirect" url="https://NEW_CLIENT.{C:2}/{R:1}" appendQueryString="true" redirectType="Found" />
    </rule>
</rules>

Now, I want to achieve the same thing, but by allowing our operations team to achieve the above without creating a new redirection rule, but simply by adding a new mapping entry in rewrite maps. For example, simply by doing the following:

iis-rewrite-maps-subdomain-mapping-question

I am able to get the rewrite maps to work with relative path, but it doesn't seem to match the condition when I specify something like old-client.myapp.com -> new-client.myapp.com.

Furthermore, as with the current working implementation I have without the rewrite maps, I am looking to do this without having to specifically mention "myapp.com", but to use a wildcard matching. This way, I can get the redirection rule to work in all the below scenarios:

  1. old-client.localhost -> new-client.localhost (local)
  2. old-client.uat.myapp.com -> new-client.uat.myapp.com (UAT)
  3. old-client.myapp.com -> new-client.myapp.com (PROD)
  4. etc. (our developers can specify custom url for testing purpose - e.g. mytestapp.com)

Here is my somewhat work-in-progress on achieving this:

<rewriteMaps>
    <rewriteMap name="Test">
        <add key="^(OLD_CLIENT\.)(.*)$" value="https://NEW_CLIENT.{C:2}/{R:1}" />

        <!-- if possible, my final objective is something simpler like this: -->
        <add key="OLD_CLIENT" value="NEW_CLIENT" />
    </rewriteMap>
</rewriteMaps>
<rules>
    <rule name="Using rewrite maps (test)" stopProcessing="true">
        <match url="(.*)" />
        <conditions>
            <add input="{Test:{REQUEST_URI}}" pattern="..." />
        </conditions>
        <action type="Redirect" url="..." appendQueryString="false" />
    </rule>
</rules>

Note: I have searched many existing questions in SO / other sources about this, but I believe nothing specifically touches sub-domain redirection using general-purpose rule via Rewrite Maps.

kent-id
  • 717
  • 10
  • 25
  • did you get it working? – Rory Oct 04 '20 at 21:19
  • From my reading of https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module the RewriteMap keys and values aren't able to be regular expressions. – Rory Oct 04 '20 at 21:25

0 Answers0