In this article, we will give short tutorial about how to redirect your www and non-HTTPS ASP.NET Core website. This is to make sure that your site will be ranked higher, since Google does not like different URLs, for example ones with and without “www.” to show the same content.
Redirect Request in ASP.NET Core
Please kindly install Microsoft.AspNetCore.Rewrite package from NuGet. Once you have installed it, you can redirect request. Just go to ASP.NET Core project, there is a “Configure” method, by adding rewrite rules to your application, it is possible to do redirect request.
Redirect Non-HTTPS Request
Please find “Configure” method in the Startup.cs file and edit it to contain the redirect you want. Note that you could put in conditional logic to only run this on a production environment.
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { var options = new RewriteOptions(); options.AddRedirectToHttps(301); app.UseRewriter(options); }
The library also includes a “AddRedirectToHttpsPermanent” method if you want to use that. “AddRedirectToHttps” allows you to specify the status code explictly. For SSL certificate, provider like ASPHostPortal has support Let’s Encrypt SSL and you can install it directly via their control panel. They also offer paid SSL certificate from $48/year.
Redirect WWW Request
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:
public class NonWwwRule : IRule { public void ApplyRule(RewriteContext context) { var req = context.HttpContext.Request; var currentHost = req.Host; if (currentHost.Host.StartsWith("www.")) { var newHost = new HostString(currentHost.Host.Substring(4), currentHost.Port ?? 80); var newUrl = new StringBuilder().Append("http://").Append(newHost).Append(req.PathBase).Append(req.Path).Append(req.QueryString); context.HttpContext.Response.Redirect(newUrl.ToString(), true); context.Result = RuleResult.EndResponse; } } }
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”):
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { var options = new RewriteOptions(); options.Rules.Add(new NonWwwRule()) app.UseRewriter(options); }
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.
Summary
I hope that article really helpful for you and above code can be applied on your ASP.NET Core project.