« Previous Next »

Thread: Rewrite input with parameters. (question mark ? issue)

Last post 05-11-2009 1:23 PM by ruslany. 5 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (6 items)

Sort Posts:

  • 05-04-2009, 4:44 AM

    Rewrite input with parameters. (question mark ? issue)

    Hi there fellow IIS7 people.

    Currently trying to make a rule to rewrite an input like this:

    getpic.asp?id=216303644&picsize=350

    when visiting this adress, I simply get an 404 and the rule does not trigger.

    I'm fairly convinced the problem lies with the "?" that I had to escape as "\?" to get my regEx pattern to work in URLrewrite GUI.

                        <match url="^getpic\.asp\?id=([_0-9]+)&amp;picsize=([0-9]+)" />
                        <conditions logicalGrouping="MatchAll" />
                        <action type="Rewrite" url="OrgBilleder/{R:1}.jpg?width={R:2}" />

     Now pasting this code I do see "&amp;" aswell, and that might be an issue aswell.

     Hope someone can tell me how to get the rule to trigger correctly.

  • 05-04-2009, 2:23 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 661

    Re: Rewrite input with parameters. (question mark ? issue)

    The pattern that is specified in the <match> element of the rule is evaluated agains the URL path only - without query string. This article explains that in more details: http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/#Accessing_URL_Parts_from_a_Rewrite_Rule

    For an example of a rule that rewrites based on the query string parameters, check out this article: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/, tip #9.

    http://ruslany.net
  • 05-11-2009, 11:07 AM In reply to

    Re: Rewrite input with parameters. (question mark ? issue)

     Completely forgot to give my feedback on this.

     Your trick did indeed work, seen the guide before, but did not realise it was what I was looking for.

     Regarding the syntax, especially with the conditions, how on earth did you figure that out?

    <add input="##{C:1}##_{QUERY_STRING}" pattern="##([^#]+)##_.*id=([0-9_]+)" />

    Is this common RegEx stuff? I would never have ended up with the correct solution here.

    I can add that it is near impossible to validate via the GUI, so don't rely on that for everything. Trial and error seems to be the way.

    For further reference, if anyone would be in the same needs.

     <rule name="SillyRule" stopProcessing="true">
                        <match url="getpic\.asp$" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{QUERY_STRING}" pattern="picsize=([0-9]+)" />
                            <add input="##{C:1}##_{QUERY_STRING}" pattern="##([^#]+)##_.*id=([0-9_]+)" />
                        </conditions>
                        <action type="Rewrite" url="OrgBilleder/{C:2}.jpg?width={C:1}" appendQueryString="false" />
                    </rule>

  • 05-11-2009, 1:00 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 661

    Re: Rewrite input with parameters. (question mark ? issue)

    The reason this trick was used is because the URL rewriter keeps the backreferences only to the last matched conditions. So in order to preserve the values extracted from the query string in multiple conditions it is necessary to do this kind of a trick.

    In future release we will simplify that by allowing tracking of backreferences across conditions, so you should be able to write a much simpler conditions, such as:

    <conditions logicalGrouping="MatchAll">
      <add input="{QUERY_STRING}" pattern="picsize=([0-9]+)" />
      <add input="{QUERY_STRING}" pattern="id=([0-9_]+)" />
    </conditions>
    <action type="Rewrite" url="OrgBilleder/{C:2}.jpg?width={C:1}" appendQueryString="false" />

    But for now, you have to use the workaround.

    http://ruslany.net
  • 05-11-2009, 1:18 PM In reply to

    Re: Rewrite input with parameters. (question mark ? issue)

     Which was the very first attempt looked like condition-wise, but as you say C:1 ended up as result in both conditions, making it impossible to determine which return value you actually got.

     Regarding the "test pattern" functionality, it would be nice to be able to test the entire rule, that would be something usefull to have in a future release aswell.

    And note that the initial pattern eg. "getpic\.asp$" and using the "test pattern" will fail with the following. "getpic.asp?id=216303644&picsize=350" and I assume that the $ means wildcard in this case?

     

  • 05-11-2009, 1:23 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 661

    Re: Rewrite input with parameters. (question mark ? issue)

    The pattern will not match because $ in regex means the end of string and your string has symbols after .asp.

    Agree that the test pattern rule should be more easy to use. We'll if we can do anything to improve it in future.

    http://ruslany.net
Page 1 of 1 (6 items)
Microsoft Communities