2012-10-24 87 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" />