http - ASP.NET Core URL Rewriting -
i trying redirect website www non-www rules http https (https://example.com) in middleware. used make redirection change in web.config such as:
<rewrite> <rules> <clear /> <rule name="redirect 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 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 new asp.net core , know how can make redirection in middleware? read article: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting didn't me force redirect www non-www.
install following nuget package:
microsoft.aspnetcore.rewrite
add following line:
app.usecustomrewriter();
within:
public void configure(iapplicationbuilder app, ihostingenvironment env)
before calling .usemvc
method.
and add following extensions class 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 can provide rewrite configuration.
hoops out.
best regards, colin
Comments
Post a Comment