My regex skills are limited but I have managed to come up with an expression which seems to match what I want but the problem I have is using the matched part of my expression.
Rule is as follows:
<rule name="FixDestinationCategories" stopProcessing="false">
<match url="^([^\/]+\/){2,4}category/([^\/]+\/){2}/?$" />
<action type="Redirect" appendQueryString="true" redirectType="Permanent" url="{R:1}" />
</rule>
An example urls might be as follows:
country/destinations/itineraries/itinerary/category/region/country/
another/directory/category/region/country/
Due to an issue on the site, I am trying to 301 redirect links on the site to remove the last 3 segments of the url. The number of segments before that can vary between 2 and 4. What I want to do is grab the entire path before /category/region/country/
, I also need to ensure that the resulting path ends with a trailing slash so the redirects in the above examples would end up being:
country/destinations/itineraries/itinerary/
another/directory/
However, as it is at the moment my redirect urls ends up being:
itinerary/
directory/
I've done a lot of searching for a solution but in this case I'm not sure I even know the terminology to use for my search as most of what I've read on backreferences seem to deal with single matches.