views:

126

answers:

1

I have this url:

http://www.site.com/products-book_1.aspx?promo=free

Here is what I have in my web.config for the UrlRewriter rules:

<rewrite url="~/products-(.+)_(\d+).aspx" to="~/product.aspx?pid=$2" />

What can I add to this expression to also retrieve the promo value? The end result would be http://www.site.com/products.aspx?pid=1&amp;promo=free

Or do I need to think about this a different way. Every example I see just uses expression for the values before the extension, not after. What about querystrings that need to be attached sometimes?

+1  A: 
<rewrite url="~/products-(.+)_(\d+).aspx?promo=(.*)" to="~/product.aspx?pid=$2&promo=$3" />

Actually this isn't really a good regex unless your URL will only contain the GET parameter 'promo' and none other.

EDIT

This might be slightly better: will only include promo parameter if it exists, else promo is left blank.

<rewrite url="~/products-(?:.+?)_(\d+).aspx(?:\?promo=(.*))*" to="~/product.aspx?pid=$1&promo=$2" />
Sbm007
Scott
But of course, I hadn't thought of that. Yes that works quite well indeed. I'm not that great with regular expressions so I'm afraid I don't have any useful tips :)
Sbm007