多个URL重写规则会发生冲突

时间:2012-09-07 07:36:40

标签: iis iis-7 url-rewriting url-redirection

我有以下情况与url重写规则相互冲突:

  1. 规则1:我需要将我的域名重定向到https
  2. 规则2:我需要重定向www.mydomain.com - > https://mydomain.com
  3. 规则3:我需要www.mydomain.com和mydomain.com重定向到https://mydomain.com/myfolder,但是如果我有mydomain.com/mysecond文件夹应该只重定向到https://mydomain.com/mysecondfolder
  4. 我能够实现的是将www.mydomain.com重定向到https://mydomain.com的所有内容(仅仅因为它与其他规则冲突,如果单独它正在工作)。

    我的规则是:

    <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                  <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                </rule> 
    
                <rule name="redirect to myfolder" enabled="true">
                    <match url="^$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/myfolder" />
                </rule>
    

1 个答案:

答案 0 :(得分:0)

我能够使用以下规则解决这个问题:

<rewrite>
            <rules>
            <rule name="Canonical Host Name" enabled="true" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" negate="true" pattern="^myapp\.com$" />
              </conditions>
              <action type="Redirect" url="http://myapp.com/{R:1}" redirectType="Permanent" />
            </rule>
                <rule name="HTTP to HTTPS redirect" enabled="true" stopProcessing="true">
                  <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                </rule> 
                <rule name="redirect to items" enabled="false">
                    <match url="^$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/items" />
                </rule>

            </rules>
        </rewrite>