多个IIS7 URL重写规则

时间:2010-09-02 13:09:06

标签: url iis-7 rewrite

我正在尝试编写IIS7 URL重写规则,它做了两件事:

  • 如果请求是http,请强制其为https
  • 如果网址中包含“www”,请将其删除并重定向到其他网址

在这两种情况下,我都想重定向到https://my.domain.com(替换真实域名)

我对http到https规则没有问题。此外,http://www.my.domain.comhttps://my.domain.com的情况也有效。但是,我无法开始工作的一个案例是原始请求是https://www.my.domain.com

这就是我现在所拥有的:

<rule name="r2" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll">
    <add input="{SERVER_PORT}" pattern="443" negate="false" matchType="Pattern" />
    <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" matchType="Pattern"  />        
  </conditions>
  <action type="Redirect" url="https://my.domain.com" />
</rule>

<rule name="r1" stopProcessing="true">
  <match url="(.*)" />
  <conditions logicalGrouping="MatchAll">
    <add input="{SERVER_PORT}" pattern="443" negate="true" matchType="Pattern" />
    <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" matchType="Pattern"  />
  </conditions>
  <action type="Redirect" url="https://my.domain.com" />
</rule>

了解我需要更改哪些内容才能让https://www.my.domain.com重定向到https://my.domain.com

2 个答案:

答案 0 :(得分:0)

这可能适合你

<rule name="Canonical Host Name" stopProcessing="true">
  <match url="(.*)"/>
  <conditions>
    <add input="{HTTP_HOST}" pattern="https://mydomain.com$"/>
  </conditions>
  <action type="Redirect" url="https://www.mydomain.com/{R:1}" redirectType="Permanent"/>
</rule>

答案 1 :(得分:0)

试试这对我有用

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="permalink">
                    <match url="article/(\D+)(\/)*$" />
                    <action type="Rewrite" url="http://mywebsite.com/article.aspx?id={R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>