2

I have a web-site, hosted on Azure App Service. What I'd like to do is to redirect traffic, such that an address like:

https://mysitecom//test//test2

Or:

https://mysitecom/test//test2

Would redirect to the same location:

https://mysitecom/test/test2

So far, I've tried adding some middleware to deal with this; for example:

app.Use(next => context =>
{
    if (context.Request.Path.Value.Contains("//"))
    {            
        context.Response.Redirect(context.Request.Path.Value.Replace("//", "/"), true);
        return;
    }
});

I've also tried:

var options = new RewriteOptions()
    .AddRedirect("redirect-rule/^(.*)//(.*)$", "redirected/$1/%2");                                       

app.UseRewriter(options);

Both of these work locally to an extent; however, it appears that running under Kestrel the slashes are automatically reduced to a single one. When this is deployed to Azure App Service, the following happens:

  • The slashes remain (e.g. https://mysitecom//test//test2)
  • The correct page is located
  • The middleware sees only a single slash

Please can someone explain why the middleware (or the redirect) is only ever seeing a single slash when this is deployed to Azure?

(SO wouldn't allow me to use an actual URL in the post)

Paul Michaels
  • 14,477
  • 36
  • 140
  • 249
  • 1
    What is adding the extra slashes in the first place? – HazardousGlitch Dec 18 '20 at 12:22
  • 1
    So you say that what you want to do is already done when using either Kestrel or Azure App Service, and the correct page is loaded anyway? Frankly, the real solution is to fix whatever produces the invalid URLs. Otherwise you're paying to cover up someone else's error. – Panagiotis Kanavos Dec 18 '20 at 12:27
  • You *are* literally paying for this. A `Redirect` sends a 302 response to the browser. You're paying for outbound traffic which means you're actually paying for those malformed URLs – Panagiotis Kanavos Dec 18 '20 at 12:30
  • These comments may be true; however, this clearly reveals something about Azure App Service that I don't understand. The question here was not: "how can I get this working", but rather, "Please could you explain why it's behaving as it does" – Paul Michaels Dec 18 '20 at 13:34
  • Are you hosting on Windows or Linux? – silkfire Dec 20 '20 at 12:56
  • Windows - just using App Service – Paul Michaels Dec 20 '20 at 13:18

1 Answers1

1

If your site runs on a Windows AppService, it runs in IIS. You are encountering an IIS feature (The "URL Rewrite Module", I think). If you run the same site on Linux, it will behave differently.

If you want to detect the "original" URL, you can try something like this:

// => /test/test2
var path1 = Request.Path

// => //test//test2
var path2 = HttpContext.Features.Get<IServerVariablesFeature>()["UNENCODED_URL"]

There are multiple sources for this behavior, though I am not sure how you could turn it off if you wanted to.

Alex AIT
  • 10,190
  • 3
  • 23
  • 49
  • When I run this locally under IIS Express, I can see the double slash (for both Request.Path and `path2` in your suggestion. However, when deployed to Azure App Service, there is only a single slash in either. This makes me thing that it isn't IIS that's causing this (I realise IIS Express is not exactly the same as whatever IIS Azure is running) – Paul Michaels Dec 21 '20 at 09:02
  • It can still be IIS, because the global configuration (e.g. applicationhost.config) can still be different between your local IIS Express and AppService. Like you see in my sources, this is a well-documented behavior independent of AppService. – Alex AIT Dec 21 '20 at 09:27
  • Which setting is applicationhost.config would cause the slashes to be combined? – Paul Michaels Dec 21 '20 at 09:32