We are excited to announce that the IIS.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

Use back reference value to get the value of server variable. [Answered]RSS

5 replies

Last post Aug 22, 2017 02:46 AM by Yuk Ding

  • Use back reference value to get the value of server variable.

    Aug 09, 2017 06:11 PM|asptechi|LINK

    Hi All- 

    I have made below rule which works fine. It uses server variable to construct the substitute url. 

    <rule name="Reverse Proxy to Rest" enabled="true" stopProcessing="true">
    <match url="^rest/.*" />
    <action type="Rewrite" url="{TESTARRDOMAIN}/{C:4}/{C:5}" />
    <conditions>
    <add input="{HTTP_HOST}{HTTP_URL}" pattern="^gateway(.*?)(\.internal\.test\.com|\.test\.com)/(rest)/(.+?)/(.*)" />
    </conditions>
    <serverVariables>
    <set name="TESTARRDOMAIN" value="https://arr.test.com" />
    </serverVariables>
    </rule>

    Now what i am trying to achieve is that - i want to pass server variable as back reference and at run time i want to use {C:X} to construct the substitute url. 

    In a simpler way i am trying to identify the domain based on the value of one of the back reference which is passed in url. 

    example : 

    http://gateway.internal.test.com/rest/api/TESTARRDOMAIN/core/v3/places/18632

    so here TESTARRDOMAIN is my {C:4} which i would like to use to getting domain "https://arr.test.com" and make substitute url. 

    I am not sure if there is any other way to to achieve the same. 

    Thanks in advance!!

  • Re: Use back reference value to get the value of server variable.

    Aug 10, 2017 07:23 AM|Yuk Ding|LINK

    Hi asptechi,

    I'm a bit confused about your Rewrite rule. What do you mean  "Use back reference value to get the value of server variable". If you mean set the value of <variable> section as back reference. I think it is unavailable in IIS while the back reference will not even capture this section. However, if you means that you need to modify the regex:

    gateway(.*?)(\.internal\.test\.com|\.test\.com)/(rest)/(.+?)/ to capture TESTARRDOMAIN in http://gateway.internal.test.com/rest/api/TESTARRDOMAIN/core/v3/places/18632 as {C:4}, you could use this one:

    ^gateway(.*?)(\.internal\.test\.com|\.test\.com)/([^/]+)/[^/]+/([^/]+)/(.*)

    Best Regards,

    Yuk Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.
  • Re: Use back reference value to get the value of server variable.

    Aug 10, 2017 03:32 PM|asptechi|LINK

    Hi Yuk Ding - 

    thanks for the reply, 

    My apologies and let me make it more clear. 

    my incoming url would be 

    http://gateway.internal.test.com/rest/TESTARRDOMAIN/api/core/v3/places/18632

    Now consider TESTARRDOMAIN is a server variable with value as below

    <serverVariables>
    <set name="TESTARRDOMAIN" value="https://arr.test.com" />
    </serverVariables>
    

    As per the condition evaluation 

    <add input="{HTTP_HOST}{HTTP_URL}" pattern="^gateway(.*?)(\.internal\.test\.com|\.test\.com)/(rest)/(.+?)/(.*)" />

    {C:4} will be TESTARRDOMAIN 

    Now at this time i want ARR module to consider TESTARRDOMAIN {C:4} as server variable and and use it's value 

    https://arr.test.com

    to generate the substitute url like below 

    https://arr.test.com/api/core/v3/places/18632

    I hope this is clear now. Let me know if you need more details.

    Also i am not rigid about using the server variable. if you have some other way of doing this then i am open for that as well. 

    All i want is to based on incoming request url (value of {C:4} i want to identify the domain and use that domain to construct the url. It's something like reading the key value form AppSetting and use it to construct the url. The only difference is that here the key would be part of incoming url.

  • Re: Use back reference value to get the value of server variable.

    Aug 11, 2017 09:25 AM|Yuk Ding|LINK

    Hi asptechi,

    <serverVariables>
    <set name="TESTARRDOMAIN" value="https://arr.test.com" />
    </serverVariables>

    This part will not map https://arr.test.com to TESTARRDOMAIN the incoming URL  inhttp://gateway.internal.test.com/rest/TESTARRDOMAIN/api/core/v3/places/18632.

    I'm sorry that I tested the server variable and find that the back reference could be used  with back reference like:

    <set name="RESPONSE_Server" value="{C:1}" />

    If you need to rewrite the /TESTARRDOMAIN in incoming URL, you'd better use another rule to rewrite the URL first, just create another redirect rule and modify the order list to let the new rule being triggered first.

    I wonder why do you want to let server variable changed then use back reference instead of use https://arr.test.com in action directly? You method seems make things much more complicated.

    Failed request tracing could help you understand the rewrite process:

    https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules

    Best Regards,

    Yuk Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.
  • Re: Use back reference value to get the value of server variable.

    Aug 15, 2017 02:53 PM|asptechi|LINK

    Thanks Yuk for reply - 

    The reason i am doing is because i dont wan;t to keep adding rules to my application. If i can make one rule to use server variable the way i want then i dont have to keep adding rules and it will work without any migration as well but looks like what i am trying to do is beyond ARR's capabilities. 

  • Re: Use back reference value to get the value of server variable.

    Aug 22, 2017 02:46 AM|Yuk Ding|LINK

    Hi asptechi,

    Yes, maybe you need to find another way to achieve this.

    Best Regards,

    Yuk Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.