iisurl-rewritingurl-rewrite-moduleisapi-rewriterewritemap

Rewrite Rule with Rewrite Map


I got a problem with the creation of rewrite rule which is using a rewrite map in IIS. The goal is to rewrite requests like /Japan/test.html or /Chile/test.html to jp/en/simple-test and cl/en/simple-test.

Here is the rewrite rule:

<rule name="Rewrite sites" enabled="true" stopProcessing="true">
  <match url="^/(.*)/test.html$" />
  <conditions>
    <add input="{Sites:{R:1}}" pattern="(.+)" />
  </conditions>
  <action type="Rewrite" url="{C:1}/en/simple-test" appendQueryString="false" />
</rule>

And this is my rewrite map:

<rewriteMap name="Sites">
  <add key="chile" value="cl" />
  <add key="japan" value="jp" />
</rewriteMap>

So once again, I want to dynamically translate all the languages into language codes and rewrite to language_code/en/simple-test. I want to use rewrite map as there will be more languages.


Solution

  • After few hours of tests, I was finally able to solve my problem. Here is how Rewrite Rule and Rewrite Map should look like for above conditions:

    <rule name="Rewrite sites" enabled="true" stopProcessing="true">
      <match url="(.*)/test.html$" />
      <conditions>
        <add input="{Sites:{R:1}}" pattern="(.*)" />
      </conditions>
      <action type="Rewrite" url="{C:1}/en/simple-test" />
    </rule>
    
    <rewriteMap name="Sites">
      <add key="chile" value="/cl" />
      <add key="japan" value="/jp" />
    </rewriteMap>
    

    Tip! If you don't known what is in {R:0}, {R:1}, {C:0} or {C:1} back referenced create fake rewrite action e.g.: <action type="Rewrite" url="www.test.com?a={C:1}" /> and in the a param you will see specyfic back reference content.