1

I have a requirement to rewrite URL's from an older version of my site to the format of the new version:

Old: /param1/param2/param3/param4/param5

New: /param3/param4/param5?a=param1&b=param2

The requirements for pattern matching are that param2 should be only numbers and that params 3-5 are optional.

I came up with this rule:

<match url="([^/]+)/([0-9]+)/?([^/]+)/?([^/]+)/?([^/]+)/?([^/]+)/" />
<action type="Redirect" url="{R:3}/{R:4}/{R:4}?a={R:1}&amp;b={R:2}" />

Which works OK if all params are present but if they aren't it breaks it. Examples:

/hello/world = ?a=hello&b=world
/hello/world/foo = foo?a=hello&b=world
/hello/world/foo/bar = foo/bar?a=hello&b=world
/hello/world/foo/bar/test = foo/bar/test?a=hello&b=world

Is this possible with URL Rewrite?

Nick Reeve
  • 1,658
  • 2
  • 22
  • 32
  • 1
    You can create 4 different rules for all your case. Different rules will have better performance rather than one huge regexp. You can check similar thread regarding regexp performance https://stackoverflow.com/questions/44972191/iis8-rewritemodule-urlrewrite-extremely-slow/44999655#44999655 – Victor Leontyev Sep 20 '17 at 04:19

0 Answers0