I’m currently working on a Progressive Web App(PWA) using ASP.NET Core. After creating our initial setup I used the Google Lighthouse chrome extension to check my application.
The results looked OK, I only had one failed audit: “Does not redirect HTTP traffic to HTTPS”.
Let’s fix this by adding the AspNetCore Rewrite middleware:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Rewrite; | |
var options = new RewriteOptions() | |
.AddRedirectToHttps(); | |
app.UseRewriter(options); |
If you need to specify a port, you can add some extra parameters:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var options = new RewriteOptions() | |
.AddRedirectToHttps(statusCode:301, sslPort: 44373); | |
app.UseRewriter(options); |