Simple Rewrite based on Query in Webforms [Answered]RSS

3 replies

Last post Nov 19, 2019 01:25 AM by Jalpa Panchal

  • Simple Rewrite based on Query in Webforms

    Nov 15, 2019 02:17 PM|prithee|LINK

    I am looking for the following rewrite, based on two options:

    1) example.com/meetings.aspx?meeting=health&year=19

    2) example.com/courses.aspx?course=health&year=19

    3) example.com/workshops.aspx?course=health&year=19

    The transformation I want to achieve is:

    1) example.com/meetings/health/19

    2) example.com/courses/health/19

    3) unaffected, so example.com/workshops.aspx?course=health&year=19

    If I could pose another question, would the original URL be accessible, that is typing "example.com/courses.aspx?course=health&year=19" will also work after imposing the rules.

    Appreciate any help on this, thanks.

  • Re: Simple Rewrite based on Query in Webforms

    Nov 18, 2019 06:11 AM|Jalpa Panchal|LINK

    Hi,

    You could use below URL rewrite rule:

      <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                        <match url="^meetings\.aspx$" />
                        <conditions>
                            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                            <add input="{QUERY_STRING}" pattern="^meeting=([^=&amp;]+)&amp;year=([^=&amp;]+)$" />
                        </conditions>
                        <action type="Redirect" url="meetings/{C:1}/{C:2}" appendQueryString="false" />
                    </rule>
                    <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                        <match url="^meetings/([^/]+)/([^/]+)/?$" />
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="meetings.aspx?meeting={R:1}&amp;year={R:2}" />
                    </rule>

    Regards,

    Jalpa

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
  • Re: Simple Rewrite based on Query in Webforms

    Nov 18, 2019 07:05 PM|prithee|LINK

    Excellent Jalpa,

    And for the course redirect I could duplicate the rule, and substitute course references?

  • Re: Simple Rewrite based on Query in Webforms

    Nov 19, 2019 01:25 AM|Jalpa Panchal|LINK

    for course you could use:

    <rule name="RedirectUserFriendlyURL2" stopProcessing="true">
                        <match url="^courses\.aspx$" />
                        <conditions>
                            <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                            <add input="{QUERY_STRING}" pattern="^course=([^=&amp;]+)&amp;year=([^=&amp;]+)$" />
                        </conditions>
                        <action type="Redirect" url="courses/{C:1}/{C:2}" appendQueryString="false" />
                    </rule>
                    <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
                        <match url="^courses/([^/]+)/([^/]+)/?$" />
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="courses.aspx?course={R:1}&amp;year={R:2}" />
                    </rule>

    If you want to create a similar rule you could use iis user-friendly rule template. 

    User Friendly URL - rule template

    If you feel my answer helps you, please mark it as an answer. This will help other people who face the same issue.

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue.
    If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.