IIS 7中HTTP和HTTPS的域重写

时间:2015-05-15 14:53:06

标签: redirect iis-7 web-config rewrite isapi-rewrite

我想在一个请求中执行大量301重定向,我发现this article有用。但是,我有一个问题,http://example.com重定向到https://www.example.com但是https://example.com没有重定向到https://www.example.com

以下是我的例子:

  1. SEO - 删除default.aspx(工作正常,但没有HTTPS)
    https://www.example.com/app/default.aspx => https://www.example.com/app/
  2. 但是http://www.example.com/app/default.aspx => http://www.example.com/app/

    1. 搜索引擎优化 - 删除尾部斜杠(没有工作,上面没有HTTPS)
      所有链接都以尾部斜杠代替 http://www.example.com/app => http://www.example.com/app/
      https://www.example.com/app => https://www.example.com/app/

    2. SEO - ToLower(工作正常但没有上述HTTPS)
      http://www.example.com/APP => http://www.example.com/app
      https://www.example.com/APP => https://www.example.com/app

    3. SEO - http规范重定向(100%工作)
      http://example.com/app => https://www.example.com/app

    4. SEO - https规范重定向(根本不起作用)
      https://example.com/app => https://www.example.com/app - 失败

    5. 搜索引擎优化 - 非规范重定向(不会100%工作。如果没有此规则,样式表无法加载,但也没有其他链接与上述任何内容不匹配)
      https://example.com/app => https://example.com/app/
      http://example.com/app => http://example/com/app/
      https://www.example.com/app => https://www.example.com/app/
      http://www.example.com/app => http://www.example.com/app/

    6. 这是我实施的,问题如上所述。

      <rewrite>
          <rules>
              <clear />
              <rule name="WhiteList - resources" stopProcessing="true">
                  <match url="^resources/" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                  <action type="None" />
              </rule>
              <rule name="SEO - remove default.aspx" stopProcessing="false">
                  <match url="(.*?)/?default\.aspx$" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                      <add input="{HTTP_METHOD}" pattern="GET" />
                  </conditions>
                  <action type="Rewrite" url="_{R:1}" />
              </rule>
              <rule name="SEO - Remove trailing slash" stopProcessing="false">
                  <match url="(.+)/$" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                      <add input="{HTTP_METHOD}" pattern="GET" />
                     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  </conditions>
                  <action type="Rewrite" url="_{R:1}" />
              </rule>
              <rule name="SEO - ToLower" stopProcessing="false">
                  <match url="(.*)" ignoreCase="false" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                      <add input="{HTTP_METHOD}" pattern="GET" />
                      <add input="{R:1}" pattern="[A-Z]" ignoreCase="false" />
                  </conditions>
                  <action type="Rewrite" url="_{ToLower:{R:1}}" />
              </rule>
              <rule name="SEO - http canonical redirect" stopProcessing="true">
                  <match url="^(_*)(.*)" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                      <add input="{HTTP_HOST}" matchType="Pattern" pattern="^www\.example\.com$" ignoreCase="true" negate="true" />
                      <add input="{HTTP_METHOD}" pattern="GET" />
                  </conditions>
                  <action type="Redirect" url="https://www.example.com/{R:2}" />
              </rule>
              <rule name="SEO - https canonical redirect" stopProcessing="true">
                  <match url="^(_*)(.*)" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                      <add input="{HTTP_HOST}" matchType="Pattern" pattern="^www\.example\.com$" ignoreCase="true" negate="true" />
                      <add input="{HTTP_METHOD}" pattern="GET" />
                      <add input="{HTTPS}" pattern="^ON$" ignoreCase="true" />
                  </conditions>
                  <action type="Redirect" url="https://www.example.com/{R:2}" />
              </rule>
              <rule name="SEO - non-canonical redirect" stopProcessing="true">
                  <match url="^(_+)(.*)" />
                  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                      <add input="{HTTP_METHOD}" pattern="GET" />
                  </conditions>
                  <action type="Redirect" url="{R:2}" />
              </rule>         
          </rules>
      </rewrite>
      

1 个答案:

答案 0 :(得分:0)

这不是真正的解决方案,因为我发现了IIS的实际问题。

当您使用上述规则(或https规范重定向)访问https://example.com时,如果您没有“裸域”认证,则会首先提供证书错误。如果用户选择继续,则会重定向到https://www.example.com

我认为这可能发生在“DNS协商”级别而不是IIS级别。