IIS 7重写规则 - 基于查询字符串的存在重定向

时间:2012-10-24 21:59:18

标签: redirect iis-7 web-config

是否可以根据最初请求的URL中是否存在查询字符串来使用web.config重定向?我不确定在条件中包括什么。我是web.config中处理重写/重定向规则的初学者,希望了解有关语法,参数等的更多信息。

我正在尝试做这样的事情:

<rewrite>
<rules>

<rule name="Restricted Folder with Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />

    <conditions>
    <!--redirect to a certain page if a certain querystring is present -->
    </conditions>             
<action type="Redirect" redirectType="Permanent" url="/test folder/{R:1}" />
</rule>

<rule name="Restricted Folder without Querystring" stopProcessing="true">
<match url="^test folder/(.*)" ignoreCase="false" />              
    <conditions>
    <!--redirect to another page if querystring is not present -->
    </conditions>             
<action type="Redirect" redirectType="Permanent" url="https://www.whatever.com/page.asp?url={R:1}" />
</rule>

</rules>
</rewrite>

2 个答案:

答案 0 :(得分:3)

deeholzman,我相信您的条件 - 添加输入示例您要使用“模式”而不是“matchType”。 EX:

<add input="{QUERY_STRING}" pattern="^whateverpattern"  />

<add input="{QUERY_STRING}" pattern="^whateverpattern" negate="true" />

答案 1 :(得分:2)

我在URL Rewrite Module Configuration Reference找到了答案。在web.config的重写部分,在你想要执行重定向的任何文件夹中,它会是这样的:

<rules>
    <rule name="Restricted Folder Gimbal" stopProcessing="false">
      <match url="(.*)"  ignoreCase="true" />
      <conditions>
         <add input="{QUERY_STRING}" pattern="^whateverpattern"  />
      </conditions>
      <action type="Redirect" redirectType="Permanent" url="http://www.whatever.com/file.html" />
    </rule>     
  </rules>

对于不存在的查询字符串,可以在条件中添加'negate'参数:

 <add input="{QUERY_STRING}" pattern="^whateverpattern"  negate="true" />

URL Rewrite Module Configuration Reference是一个非常方便的参考资料,遗憾的是我花了比预期更长的时间。