我正在尝试将我的网站从 www 重定向到非 www 规则以及 http 到 https (https://example.com https://example.com)在中间件中。我曾经在 web.config 中进行这些重定向更改,例如:
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Redirects to www.domain.com" patternSyntax="ECMAScript"
stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>
我是 ASP.NET Core 新手,想知道如何在中间件中进行这些重定向?我读了这篇文章:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting但这并没有帮助我强制将我的www重定向到非www。
安装以下 NuGet 包:
Microsoft.AspNetCore.Rewrite
添加以下行:
app.UseCustomRewriter();
within:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
致电之前.UseMvc
method.
并将以下扩展类添加到您的项目中:
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseCustomRewriter(this IApplicationBuilder app)
{
var options = new RewriteOptions()
.AddRedirectToHttpsPermanent()
.AddPermanentRedirect("(.*)/$", "$1");
return app.UseRewriter(options);
}
}
在 RewriteOptions 中,您可以提供重写配置。
篮球这会帮助你。
此致,
科林
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)