Question

I have this IIS rewrite rule

<rule name="Redirect rule for aliased Pages">
    <match url="^/pages/(.*)" />
    <action type="Redirect" url="{R:1}" />
</rule>

The idea is that it should redirect http://mydomain.com/pages/about-us/ to http://mydomain.com/about-us/. According to the tester in IIS in that case R:1 is about-us/ but the redirect always goes to http://mydomain.com//about-us/ (see double //). I have tried removing both forward slashes from the match URL and leaving each one in on its own and it does not seem to make any difference.

Any idea where that extra / is coming from?

Was it helpful?

Solution

The URL rewrite module starts checking after the initial trailing slash, so the following would remove the first level folder "pages":

<rule name="Redirect rule for aliased Pages">
  <match url="^pages/(.*)" />
  <action type="Redirect" url="{R:1}" />
</rule>

Testing with:

As expected.

Note that if you have the rewrite rules in a separate file, you will need to recycle the app pool or restart IIS for the changes to work.

Also, order of the rules is important too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top