Using URL Rewrite Module

Author: Ruslan Yakushev

Published on May 30, 2008 by ruslany

Updated on November 06, 2009 by ruslany

Average Rating  Rate It (7)

RSS

Introduction

The Microsoft URL Rewrite Module for IIS 7.0 provides flexible rules-based rewrite engine that can be used to perform broad spectrum of URL manipulation tasks, including, but not limited to:

  • Enabling user friendly and search engine friendly URL with dynamic web applications;
  • Rewriting URL’s based on HTTP headers and server variables;
  • Web site content handling;
  • Controlling access to web site content based on URL segments or request metadata.

Features

The Microsoft URL rewrite module includes these key features:

  • Rules-based URL rewriting engine. Rewrite rules are used to express the logic of what to compare/match the request URL with and what to do if comparison was successful. Web server and site administrators can use rewrite rule sets to define URL rewriting logic.

  • Regular expression pattern matching. Rewrite rules can use ECMA-262 compatible regular expression syntax for pattern matching.

  • Wildcard pattern matching. Rewrite rules can use Wildcard syntax for pattern matching

  • Global and distributed rewrite rules. Global rules are used to define server-wide URL rewriting logic. These rules are defined within applicationHost.config file and they cannot be overridden or disabled on any lower configuration levels. Distributed rules are used to define URL rewriting logic specific to a particular configuration scope. This type of rules can be defined on any configuration level by using web.config files.

  • Access to server variables and http headers. Server variables and HTTP headers provide additional information about current HTTP request. This information can be used to make rewriting decisions or to compose the output URL.

  • Various rule actions. Instead of rewriting a URL, a rule may perform other actions, such as issue an HTTP redirect, abort the request, or send a custom status code to HTTP client.

  • Support for IIS kernel mode and user mode output caching. IIS 7.0 output caching provides significant performance improvements for web applications. URL rewrite module is fully compatible with both types of output caching. This means that it is possible to safely cache responses for rewritten URL's and thus boost the performance of web applications that rely on URL rewriting.

  • String manipulation functions. Built-in string manipulation functions can be used to convert URLs to lowercase and to perform URL encoding and decoding.

  • Rewrite maps. Rewrite map is an arbitrary collection of name-value pairs that can be used within rewrite rules to generate the substitution URL during rewriting. Rewrite maps are particularly useful when you have a large set of rewrite rules, all of which use static strings (i.e. there is no pattern matching used). In those cases, instead of defining a large set of simple rewrite rules, you can put all the mappings between input URL and substitution URL as keys and values into the rewrite map, and then have one rewrite rule which references this rewrite map to look up substitution URL based on the input URL.

  • Failed Request Tracing support. IIS7.0 Failed Request Tracing can be used to troubleshoot errors related to URL rewriting.

  • Rule templates. Rule template is an extension for URL rewrite module user interface, that simplifies creation of rewrite rules for a particular task.  The module includes 3 built-in rule templates, plus it allows plugging in any number of custom templates.

  • UI for testing of regular expression and wildcard patterns. A GUI tool for testing rule patterns is included into the module's user interface. The tool can be used to quickly check how the regular expression or wildcard pattern works. Also, it can be used for troubleshooting and debugging of problems related to pattern matching.

  • UI for managing rewrite rules and rewrite maps. Rewrite rules and rewrite maps can be added, removed and edited by using "URL Rewrite Module" feature in IIS Manager.

  • GUI tool for importing of mod_rewrite rules. URL rewrite module includes a GUI tool for converting rewrite rules from mod_rewrite format into an IIS format.

Where to get the module

Download the x86 version for IIS 7.0

Download the x64 version for IIS 7.0

Upgrading from Go Live release

If you already have Go Live release of URL rewrite module installed then the installation package will upgrade it to RTW release. All rewrite rules in applicationHost.config and web.config files will be preserved.

ASP.NET update

The URL rewrite module installer package includes an ASP.NET update that fixes ASP.NET bugs specific to URL rewriting. In particular, the update contains fixes for the following bugs:

  1. ~ operator in ASP.NET Web server control is resolved incorrectly when using URL rewriting
  2. ASP.NET SiteMap.CurrentNode property returns null when sitemap contains virtual URLs

The update is applied only if the machine where URL Rewrite module is being installed has .NET Framework version 3.5 SP1 or higher. If required version of .NET Framework is installed after URL Rewrite module has been installed, then the ASP.NET update can be applied by re-running URL Rewrite module installer in repair mode.

For more details about these and other ASP.NET issues related to URL rewriting refer to this article.

Using the module

These articles cover the functionality of the URL Rewrite Module and explain how to use it to accomplish common URL rewriting scenarios.

Learning the basics

References and guidance

Video walkthrough

Changes since Go Live release

The following additions, changes and important bug fixes have been made to URL rewrite module since Go Live release:

  1. Added string manipulation functions UrlEncode and UrlDecode.
  2. Provided access to a raw original URL via server variable UNENCODED_URL. This server variable now contains the raw encoded URL exactly as it was requested by web browser.
  3. Added UI for configuring rewrite rules for Application Request Routing.
  4. Added support for in-place upgrade from Go Live release of URL rewrite module.
  5. Added functionality to clean up the internal cache to avoid excessive usage of memory when caching rewrite rules.
  6. Fixed a bug related to rewriting of URLs that contain non-ASCII characters.
  7. Fixed a bug in regular expression engine that caused character ranges to be evaluated incorrectly for case insensitive patterns.
  8. Included an update for IIS FastCGI module into the installer package.
  9. Included an update for System.Web.dll into the installer package.
  10. Included an update for IIS SetUri function into the installer package.

Known Issues

  1. mod_rewrite rules that use REQUEST_URI server variables may not work correctly when imported by using URL Rewrite “Import Rules…” feature. To fix the imported rules use any text editor to open the web.config file that contains those rules. Locate the <rewrite> section and then within that section replace all instances of string “{SCRIPT_NAME}” with the string “{URL}”. 
  2. Digest authentication cannot be used with URL rewrite module.
  3. ASP.NET Forms Authentication uses rewritten URL for redirection. For example, if requested URL is "/article.htm" and URL rewrite module rewrites the URL to "/article.aspx", which is protected by Forms Authentication, then ASP.NET will redirect to "/login.aspx?ReturnUrl=%2Farticle.aspx".
  4. Auto-detect mode in ASP.NET Forms Authentication uses rewritten URL for redirection. For example, if requested URL is "/article.htm" and URL rewrite module rewrites the URL to "/article.aspx", which is protected by Forms Authentication, then ASP.NET will redirect to "/article.aspx?AspxAutoDetectCookieSupport=1".
  5. UseUri mode in ASP.NET Forms Authentication uses rewritten URL for redirection. For example, if requested URL is "/article.htm" and URL rewrite module rewrites the URL to "/article.aspx", which is protected by Forms Authentication, then ASP.NET will redirect to "/(S(vy2ebt45imfkmjjwboow3l55 ))/article.aspx".
  6. ASP.NET rewrites back to the original URL when using URI-based authentication or cookie-less session state. For example, when request is made to "/(S(vy2ebt45imfkmjjwboow3l55 ))/article.htm" and URL rewrite module rewrites "/article.htm" to "/article.aspx", then ASP.NET will rewrite the URL back to "/article.htm", which may result in a "404 - File Not Found" error.
  7. IIS DefaultDocument module may redirect to rewritten URL when URL rewrite module rewrites to a folder. For example, if substitution URL in rewrite rule is "/folder1/folder2" and these folders exist on file system and have default document configured, then DefaultDocument module will redirect web client to "/folder1/folder2/", thus exposing rewritten URL. To prevent this from happening, use "/" at the end of the substitution URL when rewriting to a folder, e.g. "/folder1/folder2/"

Related Content

Comments

  1. Submitted on Jun 12 2008 by
    geekinabox
    I'm curious if you'll implement functionality to 'redirect POSTs' , too?

    I've see plenty of instances where folks want to 'redirect' all traffic to a site, be it a GET or a POST. GETs work fine w/ a standard redirect (the querystring is easily appended to the new Location), but POSTs do not. That is, if a POST is redirected, the browser does not re-POST to the new location; instead it issues a GET to the new Location, but the body of the POST data is lost. The loss of the data can -- in certain instances -- be problematic.

    The solution to redirecting a POST is pretty simple ... instead of issuing a HTTP redirect, send back a hidden HTML form to the browser with all of the name/value POST data as hidden fields, and, via client side scripting (onLoad) have the form auto-submit a new POST to the new 'redirected' URL.

    This isn't a true redirect, but serves the same end folks often want.

    Bottom-line: I think it would be valuable to bake this POST psuedo-redirect functionality into the rewrite module. Perhaps when a user selects to do a redirect, there could be a checkbox to 'Also perform scripted redirect for POSTs' (or something better worded) which would enable this behavior.
  2. Submitted on Jun 17 2008 by
    Leshchinsky
    Что же ты делаешь с нами великий и могучий ... IIS 7 ?
    http://dev.net.ua/blogs/leshchinsky/archive/2008/06/17/6339.aspx
  3. Submitted on Jun 24 2008 by
    lonevvolf
    It's great that Microsoft is providing an official module for this - there's just one very important thing missing for me:

    There's no way to extend it, for example, to read/write the customer's cookies. I would like to change the URL depending on a cookie from the customer, as well as update their cookie in certain cases. Is there anything planned for this?
  4. Submitted on Jun 24 2008 by
    lonevvolf
    Hmmm, well, reading cookies seems to work using {HTTP_COOKIE}, but I don't think writing to the response will work. In any case, I can't yet seem to select a single cookie value with a regular expression. :S
  5. Submitted on Jul 01 2008 by
    ruslany
    Lonevvolf, you are right about how to read a cookie. The cookie can be accessed by using {HTTP_COOKIE} as an input in rule's condition.

    If you cannot get the regex pattern to match the cookie value, as a first step I would recommend to enable failed request tracing (http://learn.iis.net/page.aspx/467/using-failed-request-tracing-to-trace-rewrite-rules/) and then see what the {HTTP_COOKIE} value actually contains. That way you will have a better idea about how the cookie string looks like and may be able to adjust your regex accordingly.

    There is no functionality in URL rewrite module to set the cookie for the response.
  6. Submitted on Jul 18 2008 by
    riggit
    Hi. Can the IIS7 URL rewriter be used with Cassini server? I imagine not, but I wonder how to incorporate this useful feature into the development process. The Intelligencia offering allows development with Cassini and then deployment to a web server. It would be great to use the new IIS7 version instead.
    Thanks!
  7. Submitted on Jul 18 2008 by
    mwoj
    Ruslany, when I try to install the URL Rewrite Module on my Windows Vista SP1 (with IIS 7.0) PC for testing, I get the following error: IIS Version 7.0 or greater is required to install Microsoft URL Rewrite Module for IIS 7.0.

    Any ideas how to get around this problem? Thanks.
  8. Submitted on Jul 21 2008 by
    ruslany
    Possible way to workaround the error reported by mwoj is to run the installer from the command line:

    msiexec /I rewrite_x86_TP1.msi
  9. Submitted on Oct 27 2008 by
    kevin.peno
    Hello,

    Take an example scenario URI:
    "index/goto/www/return/%2Ftickets%2Fcategory"

    The rewrite module states that it contains the original in the X_ORIGINAL_URL, but the output from that is actually:
    "index/goto/www/return/tickets/category"

    Any idea on what could cause this?

    Thanks!
  10. Submitted on Feb 06 2009 by
    lonas_halor
    Hi, a quick question for the forum:

    URL Rewrite Module for IIS 7.0 documentation states it is not a reverse-proxy, from the chaching point of view, but I am very interested to find out if it can serve an array of servers and not a single machine.

    I am trying to build the following architecture to secure the provision of a Web application:

    User's browser approaches server1 (IIS7 win 2008 64bit) with the URL https://server1/directory1/page1.php. Besides its connection to the Internet, server1 is also connncted to an internal LAN, and I want to rewrite the URL to into http://server2/directory2/page1.php. This is done to provide a layer of security by not exposing the name of the internal server2.

    Would appreciate your advice, thanks, Jonas.
  11. Submitted on Mar 24 2009 by
    thailandseo
    Ohh! Thanks. Long time I finding info about URL Rewrite.
    I follow to your site from codex.wordpress.org. Thangs again.
  12. Submitted on May 09 2009 by
    Jason Gross
    I have a question about using the Rewrite Module. First off, let me say - rock on - this thing is amazing in what it can do.

    Here's the scenario:
    All sub1.domain.com/whatever/file.aspx goes to:
    sub2.domain.com/whatever.file.aspx

    The trick I want is a nice simple rule that says "if it was HTTPS, send to HTTPS. If it was HTTP, send to sub2 as HTTP"

    What's a good regex to catch it. For now, I'm just using ^(.*)$ and sending everything to plain HTTP, non SSL. I'd like for it to persist the SSL/HTTPS or non, if I could.
  13. Submitted on May 27 2009 by
    jhms09
    Greetings Net,

    When running the installer: rewrite_1.1_x86_rtw.msi
    I get the following error message:

    This installation package cannot be installed by the Windows Installer service. You must install a Windows service pack that contains a newer version of the Windows Installer service.

    I have checked and I'm fully up-to-date on Windows Update.

    I'm running Windows Server 2003 Web Edition, SP2.

    Can anyone point me to the service pack which I need to install so that I can install URL Rewrite?
  14. Submitted on Jun 06 2009 by
    ruslany
    Jason Gross, check out this post - it should answers your question about HTTP vs HTTPS: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/
  15. Submitted on Jun 06 2009 by
    ruslany
    jhms09, URL Rewrite Module cannot be installed on Windows Server 2003. You have to have Vista, Windows Server 2008 or Windows 7 to install it. It works only with IIS 7.
  16. Submitted on Jun 06 2009 by
    ruslany
    TO ALL: PLEASE POST ALL YOUR QUESTIONS ABOUT THE MODULE HERE: http://forums.iis.net/1152.aspx. YOU WILL GET MUCH FASTER RESPONSE!
  17. Submitted on Jun 10 2009 by
    DukeNuken
    Guys. I use this module on my site and found out strange stuff.
    It doesn't work with simbol ":" in file name.
    I have original link like that "products.aspx?id=A11&coment=blablabla"
    and i change that new "A11-blablabla-products.html"
    and this module change it back and it works.

    BUT

    If in that new link exist simbol ":" or html code of it "%3a" it doesn't work.
    This is an example of those problems.
    "A11-bla:bla-products.html"
    "A11-blabla%3a-products.html"

    I use this pttern to change it "([\w]+)-([\w\%\:]+)-products.html"
    and in debug tool it work, but when i open it with IIS it said "page not found".

    I think it happend because IIS don't allow use ":" in file name,
    but this if fake file and i don't need that restriction.
    How to fix that problem?
  18. Submitted on Aug 07 2009 by
    ruslany
    DukeNuken, try configuring IIS Request Filter to allow double encoding. Place this xml into the web.config file inside of the configuration>/system.webServer>/ element:

    security>
    requestFiltering allowDoubleEscaping="true" />
    /security>
  19. Submitted on Aug 08 2009 by
    richierich112582
    Is it possible to store the rules in a SQL Server database rather than in the web.conifg?
  20. Submitted on Aug 10 2009 by
    Cavone.com
    Hello!
    Actually, in version 2 (beta) of the Url rewrite Module there is still some bugs around rewrite and postback...
    To avoid POSTBACK and rewrite issues you should add the following code into the Page_load:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    MyFormID.Action = Request.RawUrl
    '[...your code ...]
    End Sub

    Have fun ;)
  21. Submitted on Oct 15 2009 by
    andr
    Hi!

    How can I use named groups in rx pattern? Couldn't find it in documentation..
  22. Submitted on Nov 04 2009 by
    xponsewebs
    I am facing URL rewite problem at godaddy server... All hour suggestion are good but couls not help when I try to configure at godday server
    thanks
    arun

You must Log In to comment.