在IIS上重写URL:HTTP到HTTPS规则还包括非www到www重定向

问题描述:

如何修改此规则以包含非www到www重定向?在IIS上重写URL:HTTP到HTTPS规则还包括非www到www重定向

<rule name="Force Https" stopProcessing="true"> 
     <match url="healthcheck.html" negate="true" /> 
     <conditions> 
     <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" /> 
     </conditions> 
     <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" /> 
    </rule> 

有没有更好的方法来强制HTTPS站点范围?我使用ASP.NET MVC 5 IIS 8.5

我想你只需要添加另一个规则,它检查是否HTTP_HOST变量只包含你的主机没有www.前缀,并重定向如果是这样:

<!-- You first rule. Note stopProcessing is now false. --> 
<rule name="Force Https" stopProcessing="false"> 
    <match url="healthcheck.html" negate="true" /> 
    <conditions> 
    <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" /> 
</rule> 

<!-- Additional rule. --> 
<rule name="Force WWW" stopProcessing="true"> 
    <match url="healthcheck.html" negate="true" /> 
    <conditions> 
    <add input="{HTTP_HOST}" pattern="^yourdomain\.com"/> 
    </conditions> 
    <action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}{REQUEST_URI}" /> 
</rule> 

只需将上面的yourdomain.com更改为您的实际主机域名。

要回答您的其他问题,我认为URL重定向(通过URL重写)是强制HTTPS最简单的方式,而不会向您仍尝试通过HTTP访问您的站点的用户返回403。

UPDATE

在回答关于双301的评论,你可以试试这个单一的规则。我没有我的笔记本电脑在家里去验证,但我认为这将工作:

<!-- This rule will capture any http request regardless of the 'www.' prefix in the URL --> 
<rule name="Force Https" stopProcessing="true"> 
    <match url="healthcheck.html" negate="true" /> 
    <conditions> 
    <add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" /> 
    </conditions> 
    <action type="Redirect" url="https://www.yourdomain.com{REQUEST_URI}" redirectType="Permanent" /> 
</rule> 
<!-- This rule will capture https request that does not have the 'www.' prefix in the URL --> 
<rule name="Force WWW Prefix" stopProcessing="true"> 
    <match url="healthcheck.html" negate="true" /> 
    <conditions logicalGrouping="MatchAll"> 
    <add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" /> 
    <add input="{HTTP_HOST}" pattern="^yourdomain\.com$"/> 
    </conditions> 
    <action type="Redirect" url="https://www.yourdomain.com{REQUEST_URI}" redirectType="Permanent" /> 
</rule> 
+0

这样的作品,但它给出了两个301导航到http时重定向://,我宁愿只有一个 – OutFall

+0

@ N0ir,请参阅我的更新。干杯。 –

+0

获取重定向循环 – OutFall