简体   繁体   中英

IIS 7 rewrite rule - redirect based on presence of querystring

Is it possible to redirect using web.config based on the presence of a querystring in the initially requested URL? I'm not sure what to include in the the conditions. I'm a beginner at working with rewrite/redirect rules in web.config and would like to learn more about syntax, parameters etc.

I'm trying to do something like this:

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

deeholzman, I believe for your Condition - Add Input example you want to use "pattern" and not "matchType". EX:

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

or

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

I found the answer in the URL Rewrite Module Configuration Reference . In the rewrite section of web.config in whatever folder you wanted to perform the redirect from, it would go something like this:

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

For a querystring not present, you could add the 'negate' parameter to the condition:

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

The the URL Rewrite Module Configuration Reference is a very handy reference that unfortunately took me longer than expected to find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM