Search code examples
urliisurl-rewritingiis-10

IIS 10 - How to implement a rule that rewrites the path (...a bit...)


Using the IIS Application Request Routing, we will proxy requests to a preprod-server hosting services for both our dev, test, qa and stage environments. All this due to licenses issues of a 3'rd party software so we cannot have separate servers pr. environment. Therefore we will implement the "same" site/service on the preprod server with different ports and also names - prefix with the environment.

  • MyServiceTest/mycode.svc
  • MyServiceQA/mycode.svc
  • MyServiceStage/mycode.svc

But, in addtion we also have different DNS-aliases pr environment, so what I plan is for clients of the service to access the environments like this

  • myalias.test.mycorp.com/MyService/mycode.svc
  • myalias.qa.mycorp.com/MyService/mycode.svc

Question is, how can I useing the URL Rewrite module create an action that adds the environment prefix to the first parth of the path, and then keeps the rest? The typical action routing to the serverfarm has the argument for passing on the path

/{R:0}

But as described above I need to "sneak" the environment to the first part of the pat. Is this doable?


Solution

  • When you enable regular expression for match URL, then it will works for condition pattern at the same time.

    Please modify patternSyntax from wildcard to ECMAScript. enter image description here

    enter image description here

    This rule will rewrite no matter QA TEST STAGE to backend MyServiceTest/*.

                  <rule name="ARR_MyService-8052_lb" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="(.*)" />
    <conditions trackAllCaptures="false">
                            <add input="{HTTP_HOST}" pattern="myalias-test.mycorp.com" />
                            <add input="{REQUEST_URI}" pattern="/MyService(Test|QA|Stage)/(.*)" />
    </conditions>
    <serverVariables></serverVariables>
    <action type="Rewrite" url="http://myalias-test.mycorp.com-8052/MyServiceTest/{C:2}" />
      </rule>  
    

    If you want to map MyServiceTest to backend MyServiceTest and map MyServiceQA to backend MyServiceQA.

    Then you could try this

      <rule name="ARR_MyService-8052_lb" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
    <match url="(.*)" />
    <conditions trackAllCaptures="false">
                            <add input="{HTTP_HOST}" pattern="myalias-test.mycorp.com" />
                            <add input="{REQUEST_URI}" pattern="/MyService(Test|QA|Stage)/(.*)" />
    </conditions>
    <serverVariables></serverVariables>
    <action type="Rewrite" url="http://myalias-test.mycorp.com-8052/MyServiceTest{C:1}/{C:2}" />
      </rule>