0
votes

I have an IIS site with rewrite URLs to hide the port from our API users (not my choice). In other words, the main side rewrites urls to diffent sites on the same machine.

So if I visit (port 11001 is just for testing, it will eventually be on port 80):

http://apitest.mycompany.com:11001/v1/accounts
http://apitest.mycompany.com:11001/v1/swagger/docs/accounts

I want the rewrite to return the result from (respectively):

http://apitest.mycompany.com:9600/v1/accounts
http://apitest.mycompany.com:9600/v1/swagger/docs/accounts

There is only one rewrite rule in play here, yet while the second URL is rewritten and returned correctly, the first on yields a 404 error.

I can visit both the rewritten links directly and they will work. I can also make a temporary redirect based on the exact same rule which works.

My rewrite rule looks like this:

Match 
    - regex, ignore case: .*/accounts.*
Action
    - Rewrite
        - URL: 'http://apitest.mycompany.com:9600/{R:0}'
        - Append query string: true
        - Stop processing of subsequent rules: true

I've been looking at the failed rewrite log file, but I can't see what the problem is:

fr000037.xml

freb.xsl

1

1 Answers

0
votes

After two days of writing every conceivable combination of rewrite rules, I finally discovered the issue, which had nothing to do with the rule.

The problem was in my RouteConfig. It was left at the default of:

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

Meaning that any routes consisting of fewer than 4 path elements would be caught by the ASP.NET routing rather than the URL Rewrite. I hope others can elaborate on why this is, but it would seem that the order of this is:

Redirect > ASP Routing > Rewrite

I hope someone else can use this answer, as this cost me two full days of work.