2017-04-10 56 views
2

我在我的web.config中創建了一個重定向規則,將我的網站從http重定向到https。我遇到的問題是,網站上的每個鏈接都是https。我有很多鏈接到其他網站沒有SSL,因此我得到證書錯誤。這是我所做的:ASP.NET Core重定向http到https

<rewrite> 
    <rules> 
    <rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true"> 
     <match url="(.*)" /> 
     <conditions logicalGrouping="MatchAny"> 
     <add input="{SERVER_PORT_SECURE}" pattern="^0$" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> 
    </rule> 
    </rules> 
</rewrite> 

我該如何重定向https只爲我的域名,而不是我的網站上的每個鏈接?

+0

https://finalcodingtutorials.blogspot.ae/2017/03/non-www-to-www-with-http-to-https .html –

+0

看看這裏:https://neelbhatt.com/2018/02/04/enforce-ssl-and-use-hsts-in-net-core2-0-net-core-security-part-i/ – Neel

回答

1

您可以添加其他條件域以及,

<add input="{HTTP_HOST}" negate="true" pattern="localhost" /> 

「localhost」替換您的域名。

<rewrite> 
    <rules> 
    <rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true"> 
     <match url="(.*)" /> 
     <conditions logicalGrouping="MatchAny"> 
     <add input="{SERVER_PORT_SECURE}" pattern="^0$" /> 
     <add input="{HTTP_HOST}" negate="true" pattern="yourdaomainname" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> 
    </rule> 
    </rules> 
</rewrite> 

欲瞭解更多信息,

https://www.softfluent.com/blog/dev/2016/12/27/Page-redirection-and-URL-Rewriting-with-ASP-NET-Core

希望這有助於!

+0

謝謝!但是現在我的網站有問題,我有一個HTTP 500錯誤。我試圖刪除所有的重寫規則,並設置我的web.config,因爲它,但我一直有HTTP 500錯誤。我也嘗試設置一切,因爲它是,仍然沒有工作......我不知道如何解決 – Huby03

+0

請您分享有關錯誤的更多細節? –

+0

我已經爲此創建了另一個帖子,我詳細解釋了我的問題:http://stackoverflow.com/questions/43322941/asp-net-core-http-500-error?noredirect=1#comment73711409_43322941 – Huby03

6

實際上(ASP.NET Core 1.1)有一個名爲Rewrite的middleware,它包含您正在嘗試執行的操作的規則。

您可以使用它在Startup.cs這樣的:

var options = new RewriteOptions() 
    .AddRedirectToHttpsPermanent(); 

app.UseRewriter(options); 
+0

這裏有一些額外的資源https://docs.microsoft.com/en-us/aspnet/core/security/enforcing-ssl –