2012-10-24 89 views
2

是否有可能根据最初请求的URL中查询字符串的存在使用web.config重定向?我不确定要在条件中包含哪些内容。我在与改写工作是一个初学者/重定向规则在web.config中,并想了解更多关于语法,参数等IIS 7重写规则 - 基于查询字符串的存在重定向

我试图做这样的事情:

<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

我在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> 

对于查询字符串不存在,您可以在“否定”参数添加到条件:

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

的的URL Rewrite Module Configuration Reference是,不幸的是我花了比预期更长找到一个非常方便的参考。

+0

你应该改变你的代码,按@emailstudioadam评论。 – GregoryBrad

3

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

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

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