iis url将http重定向到非www https

Mar*_*mer 12 iis redirect web

我需要重定向

www.domain.dehttps://domain.de -works

http://www.domain.dehttps://domain.de -works

http://domain.dehttps://domain.de - 不起作用

rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)

Dav*_*tin 29

我认为这对您有用,搜索模式具有可选的www并使用后向引用C:2重定向,规则具有仅针对非https运行的条件.

这是模式:

"^(www\.)?(.*)$"
Run Code Online (Sandbox Code Playgroud)

哪里:

{C:0} - www.domain.de
{C:1} - www.
{C:2} - domain.de
Run Code Online (Sandbox Code Playgroud)

这是完整的规则:

  <rewrite>
    <rules>
      <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>
    </rules>
  </rewrite>
Run Code Online (Sandbox Code Playgroud)

  • @David Martin,{https}://www.domain.com到{https}://domain.com怎么样? (4认同)
  • 角色可以放在哪里?在根目录的 web.config 文件中? (2认同)
  • 如果您希望重定向停留在同一页面(保留路径信息)而不是转到网站根目录,请将操作更改为:`&lt;action type="Redirect" url="https://{C:2} /{R:1}" redirectType="Permanent" /&gt;` (2认同)

mrm*_*hal 7

接受的答案不处理特殊情况https://www.domain.de

这些规则完成了完整的工作:

    <rewrite>
      <rules>
        <rule name="Redirect to HTTPS without www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^OFF$" />
            <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="Special case for HTTPS with www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^ON$" />
            <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
Run Code Online (Sandbox Code Playgroud)