0

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&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 Answer 1

1
<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" />
Sign up to request clarification or add additional context in comments.

2 Comments

thank you!! I was able to get what I wanted, but I used: <rewrite url="~/Product-(.+)_(\d+).aspx\?(.+)" to="~/product.aspx?pid=$2&amp;$3" /> This allows me to use other GET parameters as well. But I'm going to look at your EDIT closely for some additional tips.
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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.