Setting Server Variables

URL Rewrite 2.0 introduced the ability to create or set your own server variables. This enables you to set custom information that will be available to other rules and to the website itself. You can set variables like HTTP_X_ORIGINAL_HOST, or pretty much any other server variable, pre-existing or made up.

The value of the server variable can be a literal string, or you can use other server variables or back-references. This gives you full flexibility in setting server variables.

Figure 19.7 shows two example server variables: one set with a server variable and one set with a literal string.

Following is an example of a rule that has the two server variables set. Notice that this is a good place to use the None action, if you don't want to perform an action on the rule.

<rule name="Set Server Variables">
   <match url=".*" />
   <serverVariables>
      <set name="HTTP_X_ORIGINAL_HOST" value="{HTTP_HOST}" />
      <set name="HTTP_X_SERVER_FARM" value="ServerFarm01" />
   </serverVariables>
   <action type="None" />
</rule>

When running this rule, you may receive a 500.50-URL Rewrite Module Error. This will occur if you created the rule at the site level, rather than the server level, and the two variables haven't been granted permission to run at the site level. The upcoming “Allowed Server Variables” section explains how to approve server variables to be changed at ...

Get Professional Microsoft IIS 8 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.