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

时间:2016-01-31 05:00:32

标签: asp.net iis https url-rewriting

如何修改此规则以将非www包含在www重定向中?

SELECT * FROM (
  SELECT type, date, ROW_NUMBER()
  OVER (PARTITION BY type ORDER BY date DESC) AS row FROM t
  ) tmp
WHERE tmp.row=2;

是否有更好的方法可以在全站点强制使用HTTPS?我在IIS 8.5上使用ASP.NET MVC 5

1 个答案:

答案 0 :(得分:1)

我认为您只需添加另一条规则来检查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更改为您的实际主机域名。

要回答您的其他问题,我认为网址重定向(通过网址重写)是强制HTTPS的最简单方法,无需向仍尝试通过HTTP访问您网站的用户返回403。

<强>更新

在回复您对双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>