Bharat Sisode Bharat Sisode - 2 months ago 8
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:
http://constant.com/caam/verifying/?token=kpG1TwYo2KqTS%2bKg%2fY6lVm2Gt

Need to convert it to URL:
http://constant.com/caam/verifying/default.aspx?token=kpG1TwYo2KqTS%2bKg%2fY6lVm2Gt

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

Answer

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" />
                </conditions>
                <action type="Redirect" url="/caam/verifying/default.aspx?{C:1}" appendQueryString="false" redirectType="Found" />
            </rule>

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.

EDIT

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