2017-04-14 20 views
1

私のウェブサイトをwwwからnon-wwwルールにリダイレクトしようとしています。またhttpをhttps(https://example.com)にミドルウェアでリダイレクトしようとしています。私はasp.netのコアに新しいですし、私は私のミドルウェアでそれらのリダイレクトを行うことができますどのように知っていただきたいと思いASP.NETコアURL書き換え

<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> 

:私は、次のようなweb.configファイルでそれらのリダイレクト変更を行うために使用されますか?私はこの記事を読んでいます:しかし、それは私のwwwをwww以外のものにリダイレクトするのを助けませんでした。

答えて

2

次NuGetパッケージのインストール:.UseMvcメソッドを呼び出す前に

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 

:内

app.UseCustomRewriter(); 

Microsoft.AspNetCore.Rewrite 

は、次の行を追加します。

そして、あなたのプロジェクトに次の拡張子のクラスを追加します。RewriteOptions以内

public static class ApplicationBuilderExtensions 
{ 
    public static IApplicationBuilder UseCustomRewriter(this IApplicationBuilder app) 
    { 
     var options = new RewriteOptions() 
      .AddRedirectToHttpsPermanent() 
      .AddPermanentRedirect("(.*)/$", "$1"); 

     return app.UseRewriter(options); 
    } 
} 

あなたのリライト構成を提供することができます。

これはあなたを助けます。

敬具、 コリン

+0

はどうもありがとうございました! – Huby03