Using server variables in IIS rewirte rules

Bartosz Wesołowski 1 Reputation point
2021-07-22T15:22:34.217+00:00

Hello,

I am trying to create a rewrite rule that will proxy all requests under selected path to my search API so I created a rewrite rule like this:

<rule name="Rewrite Search requests to SOLR">
    <match url="^search-api" />
    <action type="Rewrite" url="http://dev.search-api/select" logRewrittenUrl="true" />
</rule>

I have multiple environments, each is pointing to different search server, so lets say on the first env I need to proxy to
http://dev.search-api/select and on the other one I need to proxy to http://int.search-api/select

I want to keep my web.config file in source control and use the same file for my all envs so my question is: is there a way to use some kind of custom variable in the rewrite rules so I would have rule like this:

<rule name="Rewrite Search requests to SOLR">
    <match url="^search-api" />
    <action type="Rewrite" url="http://{MY_VARIABLE_SET_SOMEWHERE}/select" logRewrittenUrl="true" />
</rule>

and then just set MY_VARIABLE_SET_SOMEWHERE somewhere ?

Could you please advise ?

Windows development Internet Information Services
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. HuryShen - MSFT 321 Reputation points
    2021-07-23T05:58:58.617+00:00

    Hi @Bartosz Wesołowski ,

    As far as I know, we can't implement this in web.config. We can just define a key-value in <appSettings> and then use the value in xxx.cs file. But we can not define a variable somewhere and use the variable in rewrite rule in web.config.

    For this requirement, I think you can just edit the rule of your web.config. Or if you do not want to modify the web.config, you can also do it in "global.asax".


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.