[英]ASP.NET Core URL Rewriting
I am trying to redirect my website from www to non-www rules as well as http to https ( https://example.com ) in the middleware. 我正在尝试将我的网站从www重定向到非www规则以及中间件中的http到https( https://example.com )。 I used to make those redirection change in the web.config such as:
我曾经在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>
I am new to asp.net core and would like to know how can i make those redirection in my middleware? 我是asp.net核心的新手,想知道如何在我的中间件中进行这些重定向? I read this article: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting but it didn't help me to force redirect my www to non-www.
我读了这篇文章: https : //docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting但它没有帮助我强制将我的www重定向到非www。
Install the following NuGet package: 安装以下NuGet包:
Microsoft.AspNetCore.Rewrite
Add the following line: 添加以下行:
app.UseCustomRewriter();
within: 内:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Before calling .UseMvc
method. 在调用
.UseMvc
方法之前。
And add the following extensions class to your project: 并将以下扩展类添加到您的项目中:
public static class ApplicationBuilderExtensions
{
public static IApplicationBuilder UseCustomRewriter(this IApplicationBuilder app)
{
var options = new RewriteOptions()
.AddRedirectToHttpsPermanent()
.AddPermanentRedirect("(.*)/$", "$1");
return app.UseRewriter(options);
}
}
Within RewriteOptions you can provide your rewrite configuration. 在RewriteOptions中,您可以提供重写配置。
Hoops this will help you out. 篮球这将帮助你。
Best regards, Colin 最好的问候,科林
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.