• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 814
  • Last Modified:

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

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 client.test.com, test.com, and variables.test.com 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 "client.test.com", 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!
Christian de Bellefeuille
Christian de Bellefeuille
  • 2
1 Solution
Dan McFaddenSystems EngineerCommented:
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 (http://www.test.com) 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 = client.test.com
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="f1.test.dpa.com" />
                        <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

Dan McFaddenSystems EngineerCommented:
Any additional info on this question?

Christian de BellefeuilleProgrammerAuthor Commented:
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 /.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now