2017-04-10 90 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 –