0

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.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206

2 Answers2

1

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"/>
rtpHarry
  • 13,019
  • 4
  • 43
  • 64
  • hmm it worked in Expresso (a regex dev tool). when you say ppc="" do you mean the rewrite works and sends you to the page? Its just occurred to me that we have created an infinite loop because /letterchain.aspx?ppc= matches the left hand portion. Try adding processing="stop" as an attribute. – rtpHarry Dec 04 '10 at 10:34
  • Also if you are trying to match extensionless urls with url rewriting you need to either be on iis7 or setup wildcard mapping for iis6 (http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-Extensions-in-ASP.NET.aspx) – rtpHarry Dec 04 '10 at 10:42
  • Apologies your suggestion does work. When I saw your example I thought it was the same as mine - I didn't spot the extra parenthesis around the .+ Thank you very much – m.edmondson Dec 05 '10 at 17:49
0

Please note that if you are developing for IIS 7+ http://www.iis.net/download/urlrewrite/ is a module from Microsoft that performs faster rewrites with lower footprint.

BTW, your regex has a small problem, you need to escape the dot character, that is "/(?!Default.aspx)(.+)"

aracntido
  • 253
  • 3
  • 6