Search code examples
c#asp.net-coreurl-rewriting

ASP.NET Core: URL rewriting does not work with a static file


app.UseRewriter(new RewriteOptions().AddRedirect("login", "login.html")); //works
app.UseRewriter(new RewriteOptions().AddRewrite("login", "login.html", false)); //doesnt work

In the second case i get error 404, page not found. Why? The file is in the wwwroot directory.


Solution

  • Be sure call app.UseRewriter before app.UseStaticFiles(), otherwise the RewriteMiddleware might be superseeded by the one in charge of the static files, which could mess up your rewrite rules:

    app.UseRewriter(new RewriteOptions().AddRewrite("login", "login.html", false)); 
    app.UseStaticFiles();