URL Rewriting with Microsoft Rewrite Module for IIS7

Description Article

The URL Rewrite module can perform URL manipulation tasks, like:

  • Define powerful rules to transform complex URLs into simple and consistent Web addresses;
  • Easily replace Web application URLs to produce user and search engine friendly results;
  • Rewrite URLs based on HTTP headers and IIS server variables;
  • Perform redirects, send custom responses, or stop HTTP requests based on the logic expressed in the rewrite rules;
  • Control access to Web site content based on URL segments or request metadata.

The IIS 7 Rewrite module defines URL rewrite rules in the “Web.config” file.

The module provides regular expression-based replace syntax.

It includes a friendly UI for the IIS 7 Management console where its possible to:

  • Visualize all rewrite rules installed; and
  • Test the defined rules.

How to do it

The regular expression (RegEx) is  based on the virtual directory (it will be the base path). Therefore any subdirectory paths in the URL must be considered when defining the rule pattern.

Explicit groups are used to capture the required URL segments so taht they can be used in the rewrite operation. The matching groups can be referenced as {R:1} for the first group match.

The reference {R:0} is the entire match.

The image bellow shows a definition rule that allows toinvoke a WCF service without the “svc” extension (http://localhost/Training/wcfIIS70WebHost/TestService1.svc?op=1)

The “web.config” file will be automatically altered with the rules definitions:


    <modules runAllManagedModulesForAllRequests="true" />
                <rule name="WCF" stopProcessing="true">
                    <match url="^TestService1(.*)$" />
                    <action type="Rewrite" url="TestService1.svc{R:1}" />