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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top