Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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/).

share|improve this answer
    
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. –  Paolo Tedesco Dec 21 '11 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. –  Marco Miltenburg Dec 21 '11 at 18:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.