IIS URL Rewrite to do 2 actions: Set a ServerVariable, then redirect

Posted on 2016-11-02
Last Modified: 2016-11-29
I've a hard time understanding how URL rewrite work and need your help.

I want to set a server variable "HTTP_BRANDING" to some value, then redirect to another page which is showing ServerVariables to see that value, and all other servervariables values.  

For my test purpose, i've created an entry in my hosts file so,, and point to
I've also created the server variable HTTP_BRANDING by clicking "View Server Variables" in IIS.

I've written one rule:
Match Url
  • Requested URL: Matches the pattern
  • Using: Regular expressions
  • Pattern: (.*)
  • Logical Grouping: Match All
  • Input: {URL}
  • Type: Matches the patttern
  • ^/$
Server Variables
  • value: "HELLO!"
  • Replace: True
  • Action type: Redirect
  • Append query string: checked
  • Redirect Type: 301

So with this rule, i expect that just by typing "", it would be triggered since server variable "URL" only contain "/".

This partially work.  It redirect to the ShowServerVariables, but this page show HTTP_BRANDING, and it's empty, while most other server variables contain some values.

So my question is Why it doesn't get assigned?

Does anyone have a good tutorial about URL Rewrite? (Something taking few minutes to read, not a 400 pages manual.  I don't plan to launch a rocket to the moon with it, just to do this simple task)

Thank you!
Question by:cdebel
  • 2
LVL 27

Accepted Solution

Dan McFadden earned 500 total points
ID: 41872135
First off, for custom server variable, you need use an _X_ designator.  So your server variable should to be named "HTTP_X_BRANDING"

Do you really need the redirect or is it only to test that the server variable is there?  Reason I ask, is the rule above matches only if someone types the "/" in the browsers address field.  If I type just the host name ( there won't be a match on the rule.

Plus, when the redirect fires, the rule will again fire, and since the URL field does not match "/" (the URL field would be /ShowServerVariables.aspx) the server variable will not be added.

I have this working using the {SERVER_NAME} where it will also match when on the site, no matter what page or path depth.

Another thing, you need to tell IIS that specific server variables are rewrite-able.  So the process is the following:

1. In IIS Manager, go to the site you want to add the server variable to
2. Go into the URL Rewrite feature
3. In the Actions panel (right side) click "View Server Variables"
4. Add the server variable(s) that you want to be rewrite-able
5. In the Actions panel, click "Back to Rules"
6. In the Actions panel, click "Add Rule(s)"
7. HIghlight Inbound rules > Blank rule, click OK
8. Name your rule
9. Under Match URL
9a. Requested URL = Matches the Pattern
9b. Using = Regular Expressions
9c. Pattern = .*
9d. Ignore case = checked
10. Under Conditions
10a. Local grouping = Match All
11 Add Input
11a.  Condition input = {SERVER_NAME}
11b. Check if input string = Matches the Pattern
11c.  Pattern =
11d. Ignore case = checked
12. Under Server Variables
12a.  Click Add, when you pull down the "Server variable name" drop-down, your variable should be listed.  Select the variable from the list that you want.
12b. Value = HELLO!
12c.  Replace the existing value = checked
13. Under Action
13a. Action type = None

After the above config is applied, your web.config should have something like this in it:

<?xml version="1.0" encoding="UTF-8"?>
                <rule name="Add Brand Server Variable">
                    <match url=".*" />
                        <add input="{SERVER_NAME}" pattern="" />
                        <set name="HTTP_X_BRANDING" value="IB-Windows" />
                    <action type="None" />

Open in new window

If you hit your ShowServerVariable page, you should see a server variable named "HTTP_X_BRANDING" ... somewhere on in the content.

I'll assume you're using a page similar to this:

<% @ Page Language="C#" %>
foreach (string var in Request.ServerVariables)
  Response.Write(var + " " + Request[var] + "<br>");

Open in new window

LVL 27

Expert Comment

by:Dan McFadden
ID: 41891630
Any additional info on this question?

LVL 10

Author Comment

ID: 41906538
Sorry for the long time for my answer.  We changed our approach to avoid the server variables.  It's now working the way we did it.

But to answer your question, this was for test purpose so there was no need to redirect for the moment, but at the end our goal was to redirect, and have the server pass this variable to the other web site, which is hosted on the same server.

I already did the steps you described from 1 to 4a as i've mentionned in the original post.

Your assumption for the foreach loop was correct.  I'm using similar code to display every variables.

The only difference i see with what i did, is removed the redirection.  I'll have to do some other tests but i assume i did something wrong in my regex condition in the rules with the /.

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question