azureazure-front-door

URL Rewrite in Azure Front Door (Preview) Standard/Premium


We have 2 app services: foo.azurewebsites.net and bar.azurewebsites.net and have configured origin groups for both sites. We would like the front door to do path based routing: i.e.:

myfd.z01.azurefd.net/foo -> foo.azurewebsites.net
myfd.z01.azurefd.net/bar -> bar.azurewebsites.net

We can configure this to work using Patterns to match on the routes as /foo/* and /bar/* respectively. This works as expected.

Next, we want to rewrite the URL so that we don't get the initial /foo or /bar path sent to the web apps. In other words, the current setup produces the following:

myfd.z01.azurefd.net/foo/abc -> foo.azurewebsites.net/foo/abc
myfd.z01.azurefd.net/bar/def -> bar.azurewebsites.net/bar/def

What we want is:

myfd.z01.azurefd.net/foo/abc -> foo.azurewebsites.net/abc
myfd.z01.azurefd.net/bar/def -> bar.azurewebsites.net/def

So we set up a rewrite URL rule as follows:

Setting Value
Action URL rewrite
Source pattern /foo/
Destination /
Preserve unmatched path Yes

However, this does not appear to work. In the AzureDiagnostics logs we can see the rule is firing, but the URL is not being rewritten - it still contains /foo/. Is there something we are missing?


Solution

  • Problem analysis

    I came across the same issue and after some experiments I derive the following behavior for the current state of the Azure Front Door (Preview) Standard/Premium:

    If in the Endpoint Manager a Route is configured to match a sub path, e.g. /foo/*, to route to a specific Origin, an enabled UrlRewrite Rule is only applied for the path which is following /foo. e.g.

    Route:
      Pattern to match: /foo/*
      OriginGroup: Foo
    Rule: UrlRewrite
      Source pattern "/"
      Destination "/bar/"
      Preserve unmatched path: true
    

    will result in a rewrite from e.g. /foo/baz to /foo/bar/baz and thus will not work for the given issue. However, there are two options to accomplish the desired result.

    Solutions

    Way 1: Use the Origin path in the Route config instead of a Rule

    The Origin path overwrites the Pattern to match and is thus itself already an UrlRewrite. Nevertheless, via the Azure Portal UI it is not allowed to set only / as path, but via the az afd route update CLI it is possible and works:

    Update 2022 - you can now also set '/' as origin path via Azure Portal

    screenshot of azure portal route config

    No extra Rules are required here.

    Way 2: Don't use different Routes but Rules with Origin group overwrite

    Only one Route is configured matching all requests /* and serving a default Origin. To this route a Rule set is applied which contains for each origin an Origin group overwerite and a * Url rewrite* rule: enter image description here