Why Redirect Requests?
A website can be accessed using various sub-domains, CNAMEs and protocols. The problem with this is the question of where the true content resides.
Google does not like different URLs, for example ones with and without "www." to show the same content. They will penalize your site for doing this, it's duplicate content. The same is true with HTTPS.
The best practice is to 301 redirect all the alternative versions to a canonical one. You want to 301 redirect because it means permanently moved, as opposed to 302, which is meant only temporarily. A 301 redirect is essential if SEO matters at all for your website.
Redirecting Requests in ASP.NET Core
In ASP.NET Core, a request can be processed in code rather than relying on config files and the server, such as with a web.config file. This is much better because it allows adding conditional logic and gives more control on the response.
In an ASP.NET Core project, there is a "Configure" method, by adding rewrite rules to the application here, it is possible to redirect requests. To do this type of rewriting, install Microsoft.AspNetCore.Rewrite package from NuGet. Once installed, you can redirect requests.
Redirecting Non-HTTPS Requests
Redirecting WWW Requests
There is no built in method to redirect "www." requests so this requires writing a custom class which can be used with the rewrite options, the class implements the "IRule" interface. Create the following class somewhere in the web project:
Note that it's the second parameter in "Response.Redirect" which is responsible for setting "permanent" to "true". This is what is responsible for the 301 redirect. In this example, the redirect goes to the HTTP version, not the HTTPS version, you can easily edit that if you want to save on another redirect.
Once the class is in your project, add the rule to the options (add before calling "UseRewriter"):
By using this class as a new rule, a permanent redirect takes place for "www." traffic to the non-WWW version. If you needed to, you could reverse the logic and redirect non-WWW to the "www." version by editing the class.