URL Rewriting – Part 1: The Basics


Sometime ago I’ve been asked to deliver a rewrite/redirect engine solution for a project. I started researching, got some tips from my boss and found a few IIS/.NET solutions to do this kind of operation: IIS URL Rewrite ModuleURLRewriter.Net and URLRewriting.Net.

Doing this task I realized there is not much information about URL rewriting in general and even less articles about using it with SharePoint 2010. I then decided to write a series of articles on how to use it, how to configure it on IIS and apply the URL rewrite concept to SharePoint 2010.

 

What is URL Rewriting?

URL Rewriting is the operation of change the external URLs for a web site in order to allow it to have more meaningful and search friendly URLs. It can make your site easier to use and also make it more appropriate (or friendly) for the search robots crawling it.

As more web sites become dynamic and data driven, some of them expose really confusing URLs for either people to memorize and understand or for search robots to analyze. URL Rewriting can help sole this problem allowing site admins to expose URLs that actually represent the page’s content.

 

Here some examples of hard to remember and understand URLs:

https://myspworld.wordpress.com/post.php?post-424&action=view

http://www.google.com/search?sugexp=chrome,mod=9&sourceid=chrome&ie=UTF-8&q=url+rewriting

http://yourwebsite.com/products.aspx?prodid=62

 

Here are some examples of friendly URL:

https://myspworld.wordpress.com/2012/05/28/url-rewriting-part-1-the-basics

http://en.wikipedia.org/wiki/Rewrite_engine

http://yourwebsite.com/books/sharepoint-2010

 

Which URLs would you prefer to have on your web site?

 

URL Rewriting can be done in two ways: rewrite or redirect.

On the rewrite operation we have the actual meaning on the URL rewriting. We are hiding our internal complex URL and exposing a friendly URL. When the friendly URL is requested we translate it to our internal URL (full of parameters and numbers nobody but our site would understand) and execute it. The user’s browser will receive the returned HTML with the friendly URL.

On the redirect operation we expose a friendly URL but instead of executing our internal URL and keeping the friendly URL for the user’s browser, the user is redirected to a different URL, a friendly or not so friendly one, depending on how we configure the redirect.

On the browser’s level the difference between rewrites and redirects is how the URL will be displayed. On the rewrite, the browser will keep the friendly URL all the time. On the redirect, IIS will send a HTTP request for a new URL back to the browser, the browser will request the new URL and the browser’s address bar will be updated with the new URL. The user will definitely notice that the URL has changed.

 

As an example of rewrite we could have:

-The user types an url on the browser: http://yoursite.com/oldurl.

-The redirect engine detects that this URL should be changed to newurl.

-The redirect engine makes an IIS call to new url  http://yoursite.com/newurl and send its HTML output back to the browser.

-On the browser’s address bar the user will still see the old url: http://yoursite.com/oldurl.

 

As an example of redirect we could have:

-The user types an url on the browser: http://yoursite.com/oldurl.

-The redirect engine detects that this URL should be changed to newurl.

-IIS will send a HTTP request asking the user’s browser to go to  http://yoursite.com/newurl.

-The browser will request the new url and update its address bar.

 

 

IIS URL Rewrite 2.0

Analyzing a few solutions for this URL Rewriting, the one I liked better was the IIS URL Rewrite 2.0. In my case it made more sense since the run SharePoint 2010 and IIS 7 and it would be easily integrated with these technologies.

The URL Rewrite is an IIS 7 feature which allows you to process inbound and outbound URLs and redirect or rewrite it according to rules. The rules can be hard-coded, use wildcards or can even be regular expressions (I really enjoy using REGEX even though it is not that easy to create or maintain the expressions).

 

For more references on the IIS URL Rewrite check these links out:

URL Rewrite 2.0: http://www.iis.net/download/urlrewrite

URL Rewrite Extensibility Samples: http://download.microsoft.com/download/3/9/E/39E30671-7AD2-4902-B56B-C300D862595E/RewriteExtensibility.msi

Using Custom Rewrite Providers: http://learn.iis.net/page.aspx/803/using-custom-rewrite-providers-with-url-rewrite-module/

Developing a Custom Rewrite Provider: http://learn.iis.net/page.aspx/804/developing-a-custom-rewrite-provider-for-url-rewrite-module/


Installing

The installation is very simple. You just need to download the installer and run it.

Get the 32-bit installer or the 64-bit installer.

After it is installed, open IIS Manager and check a new icon on the web site’s features.

 

Using the URL Rewrite

When you click on the URL Rewrite icon on the web site’s features, it will take you to the URL Rewrite rules screen. It should be an empty screen since we don’t have any rule configured. Rules are the definitions telling IIS how to handle the URLs. Basically, we can have inbound rules and outbound rules. Inbound rules are applied when the requests come to IIS and outbound rules are applied before IIS sends HTML back to users’ browsers.

It is now time to add rules and start with URL rewriting.

 

Inbound Blank Rules

On the URL Rewrite screen, click on “Add Rule(s)…” link on the Actions panel.

Select the Inbound Blank rule template.

When creating a rule you need to define:

  • Name: rule name.
  • Match URL: defines the rules for matching URLs and applies your rule.
    • Requested URL:
      • Matches the pattern: your rule is applied if the requested URL matches the pattern.
      • Doesn’t match the pattern: your rule is applied if the requested URL doesn’t match the pattern.
    • Using:
      • Regular expressions: defines Regex as the parser for the expression defined on the pattern field.
      •  Wildcards: defines the wildcards parser as the parser for the expression defined on the pattern field.
      • Exact match: defines the URL should match exactly what is on the pattern field.
    • Pattern: defines the pattern to be searched on the URLs.
    • Ignore case: defines if your rule ignores case on the matching test. That is a important field to be selected since you have no idea on how users might type URLs.
    • Conditions:

      • Defines conditions based on the HTTP request for the rule.
      • Logical grouping:
        • Match all: requires all matches to continue processing the rule.
        • Match any: one match is enough to continue processing the rule.
      • New Condition:
        • Condition input: request variable.
        • Check if the input string: has option on how to match the input string.
        • Pattern: can be a regular or wildcard expression.
    • Server Variables

      • Allows the rule to change values of server variables.
      • Set server Variable:
        • Server variable name: variable name.
        • Value: new value.
        • Replace existing value: allows the value to be replaced if it already exists.
    • Action:

    Action types available: rewrite, none, redirect, custom response and abort request.

    Rewrite

    Defines that your rule will rewrite the URL and execute it but the URL on the user’s browser will continue the same. This execution will be transparent for the user.

    • Action Properties:
      • Rewrite URL: URL to execute instead of the original one.
        • Offers tags to be used as dynamic part of the rewrite URL:
          • {R:0}: returns the whole input expression.
          • {R:1}: returns the first part of the input URL contained in parenthesis.
          • {R:N}: returns the N part of the input URL contained in parenthesis.
      • Append query string: appends to original query string to the new URL.
      • Log rewritten URL: logs rewritten URLs to IIS log files instead of logging the URLs request by the browser.
    • Stop processing of subsequent rules: the rules are processed in the order they appear on the Rewrite Rules screen. When selecting this option your rule will abandon the processing of the following rules if this rule is applied. This normally is how you want to configure it.

    None

    No action should be taken. This is an odd option to be seem here since we can disable the rule.

    Redirect

    Defines that your rule will rewrite the URL and send the new URL back to the user’s browser. The user will notice the URL has changed and a new request will be made to IIS to process the new URL.

    • Action Properties:
      • Redirect URL: URL to redirect the browser to.
        • Offers tags to be used as dynamic part of the redirect URL:
          • {R:0}: returns the whole input expression.
          • {R:1}: returns the first part of the input URL contained in parenthesis.
          • {R:N}: returns the N part of the input URL contained in parenthesis.
      • Append query string: appends to original query string to the new URL.
      • Redirect Type:
        • Permanent (301): Redirects the browser to the new URL and tells the browser to cache it.
        • Found (302): Temporary redirect. The browser doesn’t cache it.
        • See other (303): Redirect for application using POST HTTP method.
        • Temporary (307): Also a temporary redirect introduced on HTTP 1.1. Requires the request to be repeated to be executed.

    Custom Response

    Creates a custom HTTP response to the browser with a status code defined by the rule.

    • Action properties:
      • Status code: status code number.
      • Substatus code: substatus code number.
      • Reason: string with the reason.
      • Error description: string with the error description for the response’s body.

    Abort Request

    Aborts the request and drops the HTTP connection.

     

    Example of an Inbound Rule

    In our example we are going to setup a regular expression rule to rewrite the friendly URL /products/45/sharepoint-book to our actual application URL /products.aspx?id=45&name=sharepoint-book.

    Basically we are creating a rule to get any URL composed by products/[a sequence of any characters]/[a second sequence of any  characters] and rewrite it as product.aspx?id=[first sequence of characters]&name=[second sequence of characters].

     

    Testing the example

    The product.aspx page code is very simple and just shows the two query strings ID and Name.

    The test it, first we accessed the regular URL using the parameters in the querystring.

     

    Then we tested our rewrite rule:

     

    It worked just fine. Behind the curtains IIS got the friendly URL /prodcuts/45/sharepoint-books and execute the URL /product.aspx?id=45&name=sharepoint-books.

     

    IIS UI versus Web.config

    All the options you configure on the IIS user interface actually are written on the web.config file.

    If you fill more comfortable going to the web.config file you can configure all of your rule there. It is also a much simpler way to copy the settings to move to a different server.

    All the settings are under configuration/system.webServer/rewrite node.

    Follow the web.config snippet for the inbound rule we created:

     

    On the next article I will write about outbound and user-friendly URL rules.

     

    See you,

    Amadeu.

Advertisements

2 Responses to URL Rewriting – Part 1: The Basics

  1. Pingback: URL Rewriting – Part 2: Outbound Rule « myspworld

  2. Pingback: URL Rewriting – Part 3: Integrating with SharePoint 2010 « myspworld

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: