title: ASP.NET Core 2 Url Rewriting middleware to redirect from .xxx to .yyy extension
body: <p>I want to redirect all request from .com to .net with the same path and route in dot net core.</p>
  
  <p>Same as below code for removing www from start URL:</p>
  
  <pre><code>app.UseRewriter(new RewriteOptions().Add(ctx =&gt;
  {
      // checking if the hostName has www. at the beginning
      var req = ctx.HttpContext.Request;
      var hostName = req.Host;
      if (hostName.ToString().StartsWith("www."))
      {
          // Strip off www.
          var newHostName = hostName.ToString().Substring(4);
  
          // Creating new url
          var newUrl = new StringBuilder()
                                .Append(req.Scheme)
                                .Append(newHostName)
                                .Append(req.PathBase)
                                .Append(req.Path)
                                .Append(req.QueryString)
                                .ToString();
  
          // Modify Http Response
          var response = ctx.HttpContext.Response;
          response.Headers[HeaderNames.Location] = newUrl;
          response.StatusCode = 301;
          ctx.Result = RuleResult.EndResponse;
      }
  }));
  </code></pre>