I have an ASP.NET Core 5.0 app and I've tried to use very simple code inside Startup.cs to be sure that all requests without www would be redirected to www domain (and http to https).
I've specified my domain to avoid the situation that the rule applies also to myapp.azurewebsites.net as the app is hosted in Azure (without the domain list I was getting www.myapp.azurewebsites.net). While URL http://www.example.com/Test is properly redirected to https://www.example.com/Test, for https://example.com/Test nothing happens - I get error 404 Not found. I'll appreciate any suggestions.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
string[] domains = { "example.com" };
app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent().AddRedirectToWwwPermanent(domains));
app.UseHsts();
app.UseHttpsRedirection();
(...)
}
I've found few similar questions, but they were for pre-3.0 Core and the logic of AddRedirectToWwwPermanent changed in 3.0. Maybe it'd be better to do it in web.config, but I saw some information that it's recommended to use Rewrite middleware for Core apps... Or maybe as it's hosted on Azure (Windows) I should explore option .AddIISUrlRewrite(iisUrlRewriteStreamReader)?
Side question: not sure if I should keep all three https redirections "rules": .AddRedirectToHttpsPermanent(); app.UseHsts(); app.UseHttpsRedirection(). They seem a bit overlapping, but not sure which to keep (or it's fine to have all three together).