Search code examples
url-rewritingurlrewriter.neturlvariables

Getting last part of URL into variable with Url Rewriter


I'm using Url Rewriter to create user-friendly URLs in my web app and have the following rule set up

<rewrite url="/(?!Default.aspx).+" to="/letterchain.aspx?ppc=$1"/>

How do I replace $1 so that it is the last part of the URL?

So that the following

www.mywebapp.com/hello

would transform to

/letterchain.aspx?ppc=hello

I've read the docs but can't find anything.


Solution

  • The $1 in the to portion of the group refers to the first capture group defined (eg the part in the brackets).

    The part that you actually want injecting into the $1 is the .+ which isnt in a capture group.

    I'm not sure but I think because of the (?! ) "match if suffix is absent" query this isnt counted as numbered capture group $1 so this should work:

    <rewrite url="/(?!Default.aspx)(.+)" to="/letterchain.aspx?ppc=$1"/>
    

    If it doesnt then just try inserting the second capture group into your to string instead:

    <rewrite url="/(?!Default.aspx)(.+)" to="/letterchain.aspx?ppc=$2"/>