Bharat Sisode Bharat Sisode - 1 year ago 75
ASP.NET (C#) Question

URL Rewriting - Append page name along with query string in URL

I want to add some URL rewriting stuff in my web.config

The source URL:

Need to convert it to URL:

Any ideas on how to accomplish this or other suggestions much appreciated it.

Answer Source

A basic redirect rule in IIS (web.config) might look like:

            <rule name="Token Redirect" stopProcessing="true">
                <match url="caam/verifying.*" />
                <conditions trackAllCaptures="true">
                    <add input="{QUERY_STRING}" pattern="&amp;?(token=[^&amp;]+)&amp;?" />
                    <add input="{REQUEST_URI}" pattern="default.aspx" negate="true" />
                <action type="Redirect" url="/caam/verifying/default.aspx?{C:1}" appendQueryString="false" redirectType="Found" />

You can change the match url, but basically this is matching everything that starts caam/verifying.

It then (additionally) checks that the query string has "token=" in it somewhere, and captures its value (it will go into the capture 1 as there's nothing else here, eg {C:1}).

We then output the redirect as /caam/verifying/default.aspx?{C:1} (where {C:1} is "token=12345", for example).

Note that this rule will only get hit if the URL matches (the caam/verifying part) and the parameters match (there's the "token=" part) - this redirect rule gets skipped otherwise.


I've added an additional "negate" rule to not match against the "default.aspx" page.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download