How to set up rewrite rule in Azure Web App - Linux

Viewed 457

I have an Azure Web App running on linux and i want to set up a rewrite rule for my application. When users visit example.com, i will redirect them to www.example.com.

In windows, it is easier to use web.config but how is it done in Linux ?

On the linux, there is no Apache or Nginx installed but i am wondering how the application is running.

How can i get this done ? Set up a rewrite rule on either Apache or Nginx to affect my Azure Web App running on linux ?

1 Answers

In theory, your needs can be solved in two ways.

For more details, you can read this blogs.

And also can download sample code(ASP.NET Core URL Rewriting Sample)to test it.

The first method:

Re-Writing a URL

Here's how to handle a Rewrite operation in app.Use() middleware:

app.Use(async (context,next) =>
{
    var url = context.Request.Path.Value;

    // Redirect to an external URL
    if (url.Contains("/home/privacy")) // you can use equal
    {
        context.Response.Redirect("https://markdownmonster.west-wind.com")
        //return;   // short circuit
    }

    await next();
});

The second method:

The ASP.NET Core Rewrite Middleware Module

enter image description here

The above two methods are officially provided. The actual test needs to be deployed to Azure for testing. If you have any questions, you can also raise a support ticket for help.

Related