Web.config URL重定向到非www + https

时间:2017-10-19 18:01:25

标签: redirect url-rewriting iis-7 web-config iis-7.5

目前,我的规则是:

<rule name="SecureRedirect" stopProcessing="true">
  <match url="^(.*)$" />
  <conditions>
    <add input="{HTTPS}" pattern="off" />
    <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
  </conditions>
  <action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
</rule>

问题在于:

http://www.domainName.com/image.png错误地重定向到https://domainName.com 而不是https://domainName.com/image.png

https://www.domainName.com/image.png无法重定向到https://domainName.com/image.png

那么,将所有内容重定向到非www https网址的真正方法是什么?

3 个答案:

答案 0 :(得分:1)

符合您所有要求的正确规则是:

<rule name="SecureRedirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
         <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
         <add input="{HTTPS}" pattern="off" />
    </conditions>
    <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
</rule>

答案 1 :(得分:0)

我在网站上的方式如下:

ServerName www.example.com
ServerAlias example.com
Redirect / https://www.example.com/

答案 2 :(得分:0)

尝试此规则:

<rule name="SecureRedirect" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>