It's always good to have friendly URL so that people visiting your site find it easier to remember. Asking your user to remember http://www.contoso.com/article.aspx?id=342&title=URL-Rewrite-Walkthrough for URL Rewrite walkthrough is not the nicest thing to do. Won't it be cool if user can be provided with URL like http://www.contoso.com/article/342/URL-Rewrite-Walkthrough. This indeed resonates better and is much easier to remember.

With URL Rewrite module in IIS 7.0 you can easily build this logic without having to modify your code using Rewrite Maps. I will tell you how to do this from IIS Manager. Open IIS Manager and select "Default Web Site". In the feature view click "URL Rewrite and click "Add Rules..." in the "Actions" pane. Select "Blank Rule" to start with. This will open up the "Edit Rule" property page. In order to define actual rewrite rule following has to be done:

  • Name of the rule;
  • Pattern to use for matching the URL string;
  • Optional set of conditions;
  • Action to perform if a pattern is matched and all conditions checks succeed.

    In order to make the above example work the "Edit Rule" property page should look like:

     

    The name of the rule should be unique for the rule. The pattern string "^article/([0-9]+)/([_0-9a-z-]+)" is a regular expression and will match any URL string that satisfies the below criteria:

  • Starts with the sequence of characters “article/”.
  • Contains one or more numeric characters after the first “/”.
  • Contains one or more alphanumeric or “_” or “-” characters after the second “/”.

    Since we created a rule which is supposed to rewrite URL, the rule type is "Rewrite". The Rewrite URL string "article.aspx?id={R:1}&title={R:2}" specifies new value to which input URL should be rewritten. The query parameters we used {R:1} and {R:2} are back reference to get groups as defined by rule pattern. You can learn more about back reference here. To learn more about this see this walkthrough.

    One important thing to remember is that URL Rewrite should be installed for this to work. You can install X86 version here and X64 here. This module is supported for IIS 7.0 and you should be running IIS 7.0 to take advantage of it. Also if you are working with VWD (Visual Studio for Web Developers) your project should be configured to work with IIS 7.0 and not any other web server.

    Don Raman 
    SDET, IIS Team