1

I have created 2 url rewrite rules in my web.config that look like the following:

rewrite url="~/Products/(.+).aspx" to="~/Products.aspx?Cat=$1"

rewrite url="~/Products/(.+)/(.+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"

If i type in Products/xyz.aspx it works perfectly but if i try a url that implements the second rule like Products/xyx/abc.aspx it passes both xyz and abc to the Cat and not the SubCat. Any ideas how i can get it to handle both?

Grant
  • 31
  • 2
  • 6

3 Answers3

1

I suggest removing all the matching on the "." symbol as it will make your matches a bit harder to predict. Instead use whatever character patterns cover your product categories and sub-categories.

rewrite url="~/Products/([a-zA-Z0-9]+).aspx" to="~/Products.aspx?Cat=$1"

rewrite url="~/Products/[a-zA-Z0-9]+)/[a-zA-Z0-9]+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"
Jason
  • 9,408
  • 5
  • 36
  • 36
0

Try making your regex non-greedy as:

rewrite url="~/Products/(.+?)/(.+).aspx" to="~/Products.aspx?Cat=$1&SubCat=$2"
                           ^
codaddict
  • 445,704
  • 82
  • 492
  • 529
0

That's because regular expression matching is greedy. The (.+) in the first line matches as much as it can, including xyz/abc. The quick fix for your problem is probably to change the first pattern to use ([^/]+) instead (you may have to escape the / somehow depending on your environment).

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • Hi Ben, Could you possibly give me an example of what the url rewrite would look like as regular expressions aren't one of my strong points. Thanks – Grant Dec 22 '10 at 07:55