Scripting URL rewrite module configuration

URL rewrite module fully utilizes extensibility of IIS 7.0 configuration system. The rewrite rules are stored in applicationHost.config and web.config files in XML format. This provides a huge benefit of being able to use rich set of IIS 7.0 management tools and API’s for managing and scripting URL rewriter configuration. For example, you can automate any configuration task, including creating, editing and deleting of rewrite rules, by:

The Configuration Editor, included in the Administration Pack for IIS 7.0, greatly simplifies the process of generating scripts for automating URL rewriter configuration. In this post, I will demonstrate how to use the Configuration Editor to automate configuration of rewrite rules.

Let’s assume that we want to automate the process of creating a rewrite rule for “Default Web Site”. The rule that we want to use looks like this:

<rule name="MyRule">
  <match url="[some url pattern]" />
  <action type="Rewrite" url="[some substitution url]" />
</rule>

where [some url pattern] and [some substitution url] will be the parameters set by the script.

In IIS manager,select “Default Web Site” and launch the Configuration Editor:

LaunchConfigEditor

Once in the Configuration Editor feature view UI, use the drop down list to select the “rules” configuration element, located under “system.webServer/rewrite”:

SelectRulesElement

After the selection has been made, click browse button to launch the Collection Editor:

LauncCollectionEditor

Inside the Collection Editor, click “Add” and then enter the rule configuration:

  • name: MyRule
  • match/url: foo\.htm
  • action/type: Rewrite
  • action/url: bar.htm

CollectionEditor

Close the Collection Editor and then click “Generate Script” action:

GenerateScript

This will bring up a script dialog that will contain auto-generated C# and JavaScript code as well as appcmd commands:

ScriptDialog

For example, the C# code for creating rewrite rule will look as below:

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{

    private static void Main()
    {

        using (ServerManager serverManager = new ServerManager())
        {
            Configuration config = serverManager.GetWebConfiguration("Default Web Site");

            ConfigurationSection rulesSection = config.GetSection("system.webServer/rewrite/rules");

            ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();

            ConfigurationElement ruleElement = rulesCollection.CreateElement("rule");
            ruleElement["name"] = @"MyRule";

            ConfigurationElement matchElement = ruleElement.GetChildElement("match");
            matchElement["url"] = @"foo\.htm";

            ConfigurationElement actionElement = ruleElement.GetChildElement("action");
            actionElement["type"] = @"Rewrite";
            actionElement["url"] = @"bar.htm";
            rulesCollection.Add(ruleElement);

            serverManager.CommitChanges();
        }
    }
}

Now, you can copy this code into you project and modify it as necessary for your specific configuration tasks. First thing you may want to do is change the bogus pattern and substitution URL’s with some real patterns used by your web application.

737 views

ruslany on July 27th 2008 in URLRewrite

PoorFairAverageGoodExcellent (No Ratings Yet)

Trackback URI | Comments RSS

Leave a Reply