Solved

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

Posted on 2016-11-02
3
23 Views
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 client.test.com, test.com, and variables.test.com point to 127.0.0.1.
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: (.*)
Conditions
  • Logical Grouping: Match All
  • Input: {URL}
  • Type: Matches the patttern
  • ^/$
Server Variables
  • name; HTTP_BRANDING
  • value: "HELLO!"
  • Replace: True
Action
  • 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!
0
Comment
Question by:cdebel
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
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 (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
4a.  Add HTTP_X_BRANDING
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"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Add Brand Server Variable">
                    <match url=".*" />
                    <conditions>
                        <add input="{SERVER_NAME}" pattern="f1.test.dpa.com" />
                    </conditions>
                    <serverVariables>
                        <set name="HTTP_X_BRANDING" value="IB-Windows" />
                    </serverVariables>
                    <action type="None" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

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
0
 
LVL 26

Expert Comment

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

Dan
0
 
LVL 10

Author Comment

by:cdebel
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 /.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
User art_snob (http://www.experts-exchange.com/M_6114203.html) 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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now