IIS Rewrite rule doesn't work for subfolders

Karim Darbeida 0 Reputation points
2024-11-14T15:01:53.98+00:00

Hello,

I need to enable a rewrite URL in IIS for my old TFS server ( Team Foundation Server).

The default website on 80 port, and tfs root website on 8080 work well, it rewrite the url well to my temporary website correctly, but it can't catch the tfs subfolder , I tested all the possibilities ....(only with match url, with conditions ... no way), I suspect tfs web.config right now ..

Here is my url rewrite configuration :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
                <rule name="Forward root">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="http://sptfs2:81" />
                </rule>
                <rule name="forward" enabled="true" patternSyntax="ECMAScript" stopProcessing="false">
                    <match url="^/tfs/(.*)" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="http://sptfs2:81" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
    <location path="tfs">
    </location>
</configuration>

can you tell me what I missed please ?

Thank you

Windows development | Internet Information Services
{count} votes

2 answers

Sort by: Most helpful
  1. Tom Tran (WICLOUD CORPORATION) 3,590 Reputation points Microsoft External Staff Moderator
    2025-07-11T08:03:06.88+00:00

    Hi Karim Darbeida,

    Here are a few things that might be causing the issue:


    URL Pattern Matching

    Your rule uses:

    <match url="^/tfs/(.*)" />
    

    However, IIS URL Rewrite does not include the leading slash (/) in the url pattern. This means the rule won’t match as expected, so it should be:

    <match url="^tfs/(.*)" />
    

    The rule Order and Precedence

    Your first rule (Forward root) matches everything (.*) and rewrites it to http://sptfs2:81. Since it comes before the more specific /tfs/ rule, it may be intercepting all requests — including those meant for /tfs.

    You can move the /tfs/ rule above the root rule or set stopProcessing="true" on the root rule only if you want it to short-circuit.


    Preserving the Subfolder Path

    Your current rewrite action for /tfs/ does not preserve the subfolder path. It rewrites everything to http://sptfs2:81, which likely breaks routing.

    You can try:

    <action type="Rewrite" url="http://sptfs2:81/tfs/{R:1}" appendQueryString="true" />
    

    This ensures that /tfs/some/path is rewritten to http://sptfs2:81/tfs/some/path.


    TFS-Specific Configuration

    If the above changes don’t help, it’s possible that TFS has its own web.config or HTTP modules that are interfering with the rewrite. You might want to check if there’s a web.config under the tfs folder or if TFS is using its own routing logic.


    Let me know if applying these changes helps resolve the issue.

    1 person found this answer helpful.

  2. Anonymous
    2024-11-15T08:15:54.3533333+00:00

    Hi @Karim Darbeida,

    but it can't catch the tfs subfolder , I tested all the possibilities ....(only with match url, with conditions ... no way)

    Because the first rule has already intercepted all requests(match the url)and rewritten them to "http://sptfs2:81", so it will never match the second rule.

    And I'm not sure why you're rewriting the actions of both rules to the same path, if that's what you want then you only need the first rule. But if you just want it to match subfolder paths, you can change the order of the two configured rules and add the attribute stopProcessing="true" to the forward rule.

    If I misunderstood your requirements, please describe your site structure and rewrite/redirection needs in detail.

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.