Redirecting HTTPS site to non-www in ASP.NET Core application

From the logs, it looks like you have the middleware the wrong way round

It should be

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            var options = new RewriteOptions();
            options.Rules.Add(new NonWwwRule());
            app.UseRewriter(options);
            app.UseStaticFiles();

            app.UseMvc();
        }

It should be rewriter first, then static, then MVC


And for all of you who ware crazy enough to do that in F#:

open Microsoft.AspNetCore.Http

type NonWwwRule () =
    interface IRule with
        member __.ApplyRule context =
            let request = context.HttpContext.Request
            let host = request.Host

            if host.Host.StartsWith("www.", StringComparison.OrdinalIgnoreCase) then
                let nonWwwPort = if host.Port.HasValue then host.Port.Value else 443
                let nonWwwHost = HostString(host.Host.Substring 4, nonWwwPort)
                let nonWwwPath = 
                    (sprintf "https://%s%s%s%s" 
                        nonWwwHost.Value
                        request.PathBase.Value 
                        request.Path.Value 
                        request.QueryString.Value)

                context.HttpContext.Response.Redirect nonWwwPath
                context.HttpContext.Response.StatusCode <- 301

                context.Result <- RuleResult.EndResponse
let options = RewriteOptions()
options.Rules.Add(NonWwwRule())
app.UseRewriter options |> ignore