1

I have a site at mydomain.com/mysite and I want to configure a set of redirections in IIS7.

To redirect from mydomain.com/mysite/old.aspx to mydomain.com/mysite/new.aspx I can write:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="test" patternSyntax="ExactMatch" stopProcessing="true">
        <match url="old.aspx" />
        <action type="Redirect" url="new.aspx" />
      </rule>
    </rules>

But to get the same with a rewrite map, instead, I also have to specify the /mysite/ part:

<rewriteMaps>
  <rewriteMap name="TestRewriteMap">
    <add key="/mysite/old.aspx" value="/mysite/new.aspx" />
  </rewriteMap>
</rewriteMaps>
<rules>
  <rule name="Redirect rule1 for TestRewriteMap">
    <match url=".*" />
    <conditions>
      <add input="{TestRewriteMap:{REQUEST_URI}}" pattern="(.+)" />
    </conditions>
    <action type="Redirect" url="{C:1}" appendQueryString="false" />
  </rule>
</rules>

Is there a way I can avoid specifying the site name in the rewrite map URLs?

1 Answer 1

3

I assume your put the web.config with this rewrite rule in the folder /mysite?

You should be able to replace {TestRewriteMap:{REQUEST_URI}} with {TestRewriteMap:{REQUEST_FILENAME}}. Then you you can remove /mysite/ from both the key and value in the <rewriteMap></rewriteMap>. Remember that this will also match any old.aspx deeper in the directory structure (under /mysite/).

2
  • thanks for your answer. You mean I should change this in the <condition> block? I thought that changed only when the rule is applied, but had nothing to do with the substitution. Dec 21, 2011 at 7:54
  • It's indeed the rewritemap block where you specify what should be replaced with what. However, the substitution itself happens in the rule. The rule matches any URL. The condition is just there to be able to call the rewritemap to check if a substitution is available for the given URL. If the rewritemap returns any result (pattern="(.*)"), the condition is true and thus the redirect happens, otherwise not. By not feeding the rewritemap the value of {REQUEST_URI} (which is the full URL) but the value of {REQUEST_FILENAME} it will match substitutions against only the name of the script. Dec 21, 2011 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .