Tip #31: Did you know… How to have user friendly URL using IIS 7.0 URL Rewrite module?

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

  • Comments (3)
    1. I think it’s a good idea to explain that URL Rewrite is not a standard module that ships with IIS but one you need to download from this location: http://learn.iis.net/page.aspx/460/using-url-rewrite-module/

      May save some people from searching the "features list" or the IIS setup… 😉


    2. John says:

      Is there are way to access the rewrite functionality to generate the friendly URLs for links you are throwing down on the page?

    3. @Imar: Thanks for the point. I mentioned that in my Tips. Yes it doesn’t come by default in IIS7.0.

      @John: The process is same irrespective of the fact if the link is physically typed by the user in the address bar of the browser or user is clicking the same as a link inside the page. But in my view, I don’t see any advantage of doing what you are asking for. The actual URL to which the link points will be hidden iside the page code and this can as well point to the non-trivial link. If you are thinking in terms of making your link SEO (Search Engine Optimized), you can as well use URL Redirect to make it happen. Look at Tip #25 for how to do this.



    Comments are closed.

    Skip to main content