Home > Software engineering >  IIS redirect rule not catching specified RegEx
IIS redirect rule not catching specified RegEx

Time:07-01

I am trying to configure a Redirect Rule for our IIS server.

I have examined all the documentation as well as several other examples on the site, however I cannot seem to get the requested URL to capture when the pattern matches. I tried testing the regex inside the IIS rule editor and it shows that it matches correctly. I also set up some conditions and tested them however, it seems to not work either.

here is the rule in xml

<rule name="product_detail.asp redirect to aspx" stopProcessing="true">
    <match url="http:\/\/ten\.perle\.com\/products\/product_detail.asp\?(.*)&amp;(.*)&amp;(.*)" />
        <conditions>
           <add input="{QUERY_STRING}" pattern="http:\/\/ten\.perle\.com\/products\/product_detail.asp\?(.*)&amp;(.*)&amp;(.*)" />
        </conditions>
        <action type="Redirect" url="products/product_detail.aspx?{C:1}&amp;{C:2}&amp;{C:3}" appendQueryString="false" />
</rule>

sample url http://ten.perle.com/products/product_detail.asp?a=2&i=08000514&c=1650

I don't understand what exactly I am doing wrong It appears as though my regex should catch the request but for some reason it never seems to.

CodePudding user response:

I feel like there seems to be some problem with your URL rule.

For example, Send a request to the following URL: http://ten.perle.com/products/product_detail.asp?id=123&name=test

The Pattern in the Match URL should be the path part of the URL: products/product_detail.asp

The QUERY_STRING server variable matches the part of the URL's parameter string: id=123&name=test

You can try to modify your rule and see if it works.

In addition, You can use Failed Request Tracing in IIS to troubleshoot. The log file in the FailedReqLogFiles folder can see the whole process of rewriting in detail, and can check where the match fails.

CodePudding user response:

So the Issue was solved using the below rule

<rule name="product_detail.asp redirect" stopProcessing="true">
    <match url="^(products/product_detail.asp?)([^x] )$" />
    <action type="Redirect" url="products/product_detail.aspx?{R:2}" />
</rule>

Some things That I would like to include in case anyone else faces a similar issue. Some things to clarify.

  1. the match URL portion should include the relative path of the URL you're going to be redirecting. In my initial attempts I was unsure if to include a full path or just the relative portion. This isn't fully clear in any of the documentation, at least its not explicitly stated anywhere i could find.
  2. The redirect URL needs to contain everything required after the base URL for the new URL. Including parameters if required either pulled via rules {R:x} or from the conditions {C:x}
  3. It took me some time to understand the parameter capturing, this is based on the regEx, if you test it it will show you how the url match is broken up into numbered rules based on your regEx
  4. I ended up not need the query string condition for my use case, it was simpler for me to use the rules capture from the initial pattern.

also btw was working on IIS v10.0 Hope this helps some other beginners looking to do this and clarify some more of the less obvious aspects of IIS redirects.

  • Related