c# - 在 ASP.NET Core 应用程序中将 HTTPS 站点重定向到非 www

标签 c# asp.net-core url-rewriting asp.net-core-2.0

我正在设置我的第一个 ASP.NET Core 应用程序。此应用程序在 HTTPS 上运行。目前,我有两个响应域:domain.dkwww.domain.dk,我想 301 重定向 www 非 www

我用 Google 搜索了很多,最后添加了以下类:

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("https://").Append(newHost).Append(req.PathBase).Append(req.Path).Append(req.QueryString);
            context.HttpContext.Response.Redirect(newUrl.ToString());
            context.Result = RuleResult.EndResponse;
        }
    }
}

然后我将其添加到我的 Startup.cs 中的 Configure 方法:

var options = new RewriteOptions();
options.Rules.Add(new NonWwwRule());
app.UseRewriter(options);

但是,我的网站仍然没有对此做出响应,并且 www 和没有作品。

编辑:

我添加了日志记录。在我的 ApplyRule 中,我发送了一些垃圾邮件:

    public void ApplyRule(RewriteContext context)
    {
        _logger.LogInformation("ApplyRule added");

        var req = context.HttpContext.Request;
        var currentHost = req.Host;

        _logger.LogInformation("Currenthost: " + currentHost.Host + " & URL: " + req.Path);

        if (currentHost.Host.StartsWith("www."))
        {

            _logger.LogInformation("currentHost.Host.StartsWith");

            var newHost = new HostString(currentHost.Host.Substring(4), currentHost.Port ?? 443);
            var newUrl = new StringBuilder().Append("https://").Append(newHost).Append(req.PathBase).Append(req.Path).Append(req.QueryString);
            _logger.LogInformation("newURL: " + newUrl);
            context.HttpContext.Response.Redirect(newUrl.ToString());
            _logger.LogInformation("response added: " + newUrl.ToString());
            context.Result = RuleResult.EndResponse;
            _logger.LogInformation("EndResponse added");
        }
    }

阅读我的日志,我的印象是它只被调用 JS 文件而不是主要请求。

2018-03-05 14:44:33.380 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/Content/styles/styles.css?v=RAlju8RYSVrnTxqDUl4Jj9OkakB2USJn8Hg8TkcZ2AY  
2018-03-05 14:44:33.383 +00:00 [Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/Content/styles/styles.css'. Physical path: 'D:\home\site\wwwroot\wwwroot\Content\styles\styles.css'
2018-03-05 14:44:33.383 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.8044ms 200 text/css
2018-03-05 14:44:33.638 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/Content/images/common/logo-dark.png  
2018-03-05 14:44:33.640 +00:00 [Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/Content/images/common/logo-dark.png'. Physical path: 'D:\home\site\wwwroot\wwwroot\Content\images\common\logo-dark.png'
2018-03-05 14:44:33.640 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.1224ms 200 image/png
2018-03-05 14:44:33.886 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/js/site.min.js  
2018-03-05 14:44:33.887 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.6345ms 302 
2018-03-05 14:44:34.019 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/Content/images/icons/result.svg  
2018-03-05 14:44:34.021 +00:00 [Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/Content/images/icons/result.svg'. Physical path: 'D:\home\site\wwwroot\wwwroot\Content\images\icons\result.svg'
2018-03-05 14:44:34.023 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 4.6392ms 200 image/svg+xml
2018-03-05 14:44:34.175 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/Content/images/icons/customerfocused.svg  
2018-03-05 14:44:34.175 +00:00 [Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/Content/images/icons/customerfocused.svg'. Physical path: 'D:\home\site\wwwroot\wwwroot\Content\images\icons\customerfocused.svg'
2018-03-05 14:44:34.176 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.9026ms 200 image/svg+xml
2018-03-05 14:44:34.180 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/Content/images/icons/responsible.svg  
2018-03-05 14:44:34.184 +00:00 [Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/Content/images/icons/responsible.svg'. Physical path: 'D:\home\site\wwwroot\wwwroot\Content\images\icons\responsible.svg'
2018-03-05 14:44:34.185 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 4.8705ms 200 image/svg+xml
2018-03-05 14:44:34.186 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://likvido.dk/js/site.min.js  
2018-03-05 14:44:34.186 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.527ms 404 
2018-03-05 14:44:34.265 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/Content/images/icons/professional.svg  
2018-03-05 14:44:34.265 +00:00 [Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/Content/images/icons/professional.svg'. Physical path: 'D:\home\site\wwwroot\wwwroot\Content\images\icons\professional.svg'
2018-03-05 14:44:34.265 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.855ms 200 image/svg+xml
2018-03-05 14:44:33.887 +00:00 [Information] Likvido.Website.Main.Startup: ApplyRule added
2018-03-05 14:44:33.887 +00:00 [Information] Likvido.Website.Main.Startup: Currenthost: www.likvido.dk & URL: /js/site.min.js
2018-03-05 14:44:33.887 +00:00 [Information] Likvido.Website.Main.Startup: currentHost.Host.StartsWith
2018-03-05 14:44:33.887 +00:00 [Information] Likvido.Website.Main.Startup: newURL: https://likvido.dk:443/js/site.min.js
2018-03-05 14:44:33.887 +00:00 [Information] Likvido.Website.Main.Startup: response added: https://likvido.dk:443/js/site.min.js
2018-03-05 14:44:33.887 +00:00 [Information] Likvido.Website.Main.Startup: EndResponse added
2018-03-05 14:44:34.186 +00:00 [Information] Likvido.Website.Main.Startup: ApplyRule added
2018-03-05 14:44:34.186 +00:00 [Information] Likvido.Website.Main.Startup: Currenthost: likvido.dk & URL: /js/site.min.js
2018-03-05 14:44:34.611 +00:00 [Information] Likvido.Website.Main.Startup: ApplyRule added
2018-03-05 14:44:34.611 +00:00 [Information] Likvido.Website.Main.Startup: Currenthost: www.likvido.dk & URL: /js/site.min.js
2018-03-05 14:44:34.611 +00:00 [Information] Likvido.Website.Main.Startup: currentHost.Host.StartsWith
2018-03-05 14:44:34.611 +00:00 [Information] Likvido.Website.Main.Startup: newURL: https://likvido.dk:443/js/site.min.js
2018-03-05 14:44:34.611 +00:00 [Information] Likvido.Website.Main.Startup: response added: https://likvido.dk:443/js/site.min.js
2018-03-05 14:44:34.611 +00:00 [Information] Likvido.Website.Main.Startup: EndResponse added
2018-03-05 14:44:34.714 +00:00 [Information] Likvido.Website.Main.Startup: ApplyRule added
2018-03-05 14:44:34.714 +00:00 [Information] Likvido.Website.Main.Startup: Currenthost: likvido.dk & URL: /js/site.min.js
2018-03-05 14:44:34.478 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/  
2018-03-05 14:44:34.478 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) with arguments ((null)) - ModelState is Valid
2018-03-05 14:44:34.478 +00:00 [Information] Likvido.Website.Main.Controllers.HomeController: TEST INDEX LOGGER
2018-03-05 14:44:34.479 +00:00 [Information] Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor: Executing ViewResult, running view at path /Views/Home/Index.cshtml.
2018-03-05 14:44:34.479 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) in 0.9628ms
2018-03-05 14:44:34.481 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.4825ms 200 text/html; charset=utf-8
2018-03-05 14:44:34.484 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/Content/images/pages/bg-image-2.jpg  
2018-03-05 14:44:34.485 +00:00 [Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/Content/images/pages/bg-image-2.jpg'. Physical path: 'D:\home\site\wwwroot\wwwroot\Content\images\pages\bg-image-2.jpg'
2018-03-05 14:44:34.486 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/Content/images/icons/phone-icon-3.svg  
2018-03-05 14:44:34.487 +00:00 [Information] Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Sending file. Request path: '/Content/images/icons/phone-icon-3.svg'. Physical path: 'D:\home\site\wwwroot\wwwroot\Content\images\icons\phone-icon-3.svg'
2018-03-05 14:44:34.487 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.9784ms 200 image/svg+xml
2018-03-05 14:44:34.500 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 16.1968ms 200 image/jpeg
2018-03-05 14:44:34.610 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/js/site.min.js  
2018-03-05 14:44:34.611 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 1.3489ms 302 
2018-03-05 14:44:34.713 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://likvido.dk/js/site.min.js  
2018-03-05 14:44:34.714 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.573ms 404 

最佳答案

从日志来看,您的中间件似乎弄错了

应该是

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();
        }

应该是先rewriter,再static,再MVC

关于c# - 在 ASP.NET Core 应用程序中将 HTTPS 站点重定向到非 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49023794/

相关文章:

c# - 如何将名字大写

c# - 将客户端证书添加到 .NET Core HttpClient

azure - 通过 IP 地址阻止对 url 的访问

reactjs - react + asp.net core (.net 6) 创建新 Controller

c# - DynamicRouteValueTransformer 不存在

即使在服务器端重写 URL 后,Angularjs HTML5 模式也不起作用

asp.net - IIS URL动态重写方式

c# - 在 MySQL 的结果中找不到指定的列

c# - 语法 : return _(); IEnumerable<TSource> _() 的含义

c# - LinqToSql。更新行时死锁。并行.For