Iis – Redirect URL in IIS to different domain and remove portions of the URL

iisredirectrewriteurl

We currently have URLs like:

https://tfs.mydomain.com/tfs/MyProjectCollection/Project/_workitems

that I need redirected to:

https://dev.azure.com/MyOrg/Project/_workitems

Using the HTTP Redirect module in IIS I've been able to forward requests from https://tfs.mydomain.com to https://dev.azure.com/MyOrg without any issue.

The problem is that when it's a deep link, I need to remove the /tfs/MyProjectCollection portion of the link when redirecting it. With my current setup of just redirecting the root domain, a request to:

https://tfs.mydomain.com/tfs/MyProjectCollection/Project/_workitems

ends up getting redirected to:

https://dev.azure.com/MyOrg/tfs/MyProjectCollection/Project/_workitems

instead of the desired:

https://dev.azure.com/MyOrg/Project/_workitems

This is what my web.config currently looks like:

<configuration>
    <system.webServer>
        <httpRedirect enabled="true" destination="https://dev.azure.com/MyOrg" httpResponseStatus="Permanent" />
    </system.webServer>
</configuration>

How can I strip out the /tfs/MyProjectCollection portion of the URL when redirecting it?


Update: I have also tried installing the URL Rewrite module and setting up my web.config like this, but still have the same results.

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="IQProjectCollection" stopProcessing="true">
                    <match url="^tfs/IQProjectCollection$" />
                    <action type="Redirect" url="https://dev.azure.com/iqmetrix" />
                </rule>
                <rule name="tfs" stopProcessing="true">
                    <match url="^tfs$" />
                    <action type="Redirect" url="https://dev.azure.com/iqmetrix" />
                </rule>
                <rule name="home" stopProcessing="true">
                    <match url="^$" />
                    <action type="Redirect" url="https://dev.azure.com/iqmetrix" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Best Answer

I figured out the answer by stumbling across this similar question. Using the IIS URL Rewrite module, this is what my web.config now looks like to do what I want. Note that the order of the rules does matter (most specific to least specific):

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="IQProjectCollection" stopProcessing="true">
                    <match url="^tfs/MyProjectCollection/(.*)" />
                    <action type="Redirect" url="https://dev.azure.com/MyOrg/{R:1}" />
                </rule>
                <rule name="tfs" stopProcessing="true">
                    <match url="^tfs/(.*)" />
                    <action type="Redirect" url="https://dev.azure.com/MyOrg/{R:1}" />
                </rule>
                <rule name="home" stopProcessing="true">
                    <match url="^$" />
                    <action type="Redirect" url="https://dev.azure.com/MyOrg" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>