1
votes

I'm using a RewriteMap in my web.config file to (301) redirect old URLs to new URLs. The rule is currently as follows:

    <rule name="Localhost Rewrite Rules" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^(.*\.)?example\.com$" />
        <add input="{lhRewrites:{REQUEST_URI}}" pattern="(.+)" />
      </conditions>
      <action type="Redirect" appendQueryString="false" url="{C:1}" redirectType="Permanent" />
    </rule>

Please note, the pattern intentially hard-codes the domain as this is part of a larger set of rules which are specific for each domain name. So I'm not looking to optimise or rewrite (excuse the pun) the rule in general.

The rewriteMap.config file contains the following redirect:

<rewriteMap name="lhRewrites">
  <add key="/old" value="/new" />
</rewriteMap>

Currently the rule works fine when I visit http://example.com/old - it redirects to http://example.com/new.

However, it doesn't work when the 'old' URL includes a trailing slash i.e. http://example.com/old/. I need it to work regardless of the presence of a trailing slash on the old URL.

I'd prefer not to have to duplicate all of the mapping keys.

Many thanks.

1

1 Answers

2
votes

You can achieve it if you will match URL without trailing slash in regexp ^(.*[^/])(\/?)$ and then pass this matched URL into rewrite map. Your rule should be like that:

<rule name="Localhost Rewrite Rules" stopProcessing="true">
  <match url="^(.*[^/])(\/?)$" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^(.*\.)?example\.com$" />
    <add input="{lhRewrites:/{R:1}}" pattern="(.+)" />
  </conditions>
  <action type="Redirect" appendQueryString="false" url="{C:1}" redirectType="Permanent" />
</rule>