URL Rewriting – Part 2: Outbound Rule


In this series of posts I’m going through the URL Rewriting process using the IIS URL Rewrite feature.

Check the other posts:
URL Rewriting – Part 1: The Basics

 

On the previous post we installed the feature and configured an inbound rule, which basically is a friendly URL when requests come from the browser to IIS. In this post we are going to talk about other rule templates available on the IIS URL Rewrite. One of the rules we are going to see is the outbound rule which is when we process elements on the returning HTML to be set to the browser and transform URLs into friendly URLs.

 

Outbound Rules

Outbound rules control the way links should be rendered when IIS sends HTML back to the browsers after processing the requested page. It can be used to enforce SEO links on your site (friendly URLs) and handle business rules.

 

Outbound Blank Rule

Go to IIS Manager. Select your site.

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

 

Select the Outbound Blank rule template.

When creating a rule you need to define:

  • Name: rule name.
  • Pre-Condition: the pre-condition defines if the outbound rule should be applied to the response IIS is sending to the browser. You can use different fields of the response to check on your pre-condition.

The pre-conditions can be reused between different rules.

  • Name: pre-condition name.
  • Using:
    • Regular expression: 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.
    • Logical grouping:
      • Match all: all conditions need to apply for the outbound rule to be applied.
      • Match any: if one condition applies the outbound rule is applied.
    • Conditions:

  • Condition input: HTTP header field to be checked. Examples: it can be the content type ({RESPONSE_CONTENT_TYPE}) or the response status ({RESPONSE_STATUS}).
  • Check if input string:
    • Matches the pattern.
    • Does not match the pattern.
    • Pattern: the pattern to be tested.
    • Ignore case: ignores cases when applying the pattern. It is a good setting to be left checked if you do lots of regular expression.
  • Match
    • Matching scope: defines if the outbound rule should be applied to the response header (server variables) or to the response body (response).
      • Response
      • Server variable
    • Match the content within: tags and attributes to apply the rewrite when processing the return HTML content.
    • Custom tags: specify your custom tags contained on the return HTML.
    • Content:
      • Matches the pattern.
      • Does Not Match the Pattern.
    • Using:
      • Regular Expressions: looks for a regular expression on the selected tags properties.
      • Wildcards: looks for a wildcard expression on the selected tags properties.
      • Exact Match: looks for a exact match on the selected tags properties.
    • Pattern: expression to be matched based on the type of expression selected on the Using field.
    • Ignore Case: ignores cases when applying the pattern. It is a good setting to be left checked if you do lots of regular expression.
  • Conditions
    • They are the same as pre-conditions but cannot be reused between outbound rules.
  • Action
  • Action Type:
    • Rewrite: applies the regular expression to the links found on the return HTML.
    • None: doesn’t execute any operation on the request, just closes it.
  • Stop processing of subsequent rules: stops the processing if the rule is applied.

 

Example of an Outbound Rule

This example rule is intended to modify links pointing to the product.aspx page rewrote to the /products/[product id]/[product name] format.

Here is the rule configuration:

This is the HTML code for my home page with the links to the products:

This is the returned HTML I can see on my browser:

This is how the page looks like. The rewrite feature has no effects on the page itself.

References about this topic can be found at  IIS.net.

 

Complexity of Regex

If you look at the outbound rule example you might find very complex to write Regex. Normally it is not very easy to write it if you’re not used with its syntax and it can lead you to make several mistakes and have problems debugging it.

 

If there is a tip I can give you it is: test it outside IIS and your web pages before trying to debug it in any other tool or way. A good, simple and free (some of the features are free) way to do it is using the Regex Hero web site. It has a nice UI and several features to allow you understand how the matches happen.

 

Conclusions

At this point we know how to create basic inbound and outbound rules in IIS using the IIS Rewrite module. This way we can make entire sites look friendlier for end users changing its URLs to have a more meaningful appearance. On the next articles we will look on how to create custom providers to have rules created outside of IIS and how to integrate the IIS Rewrite to SharePoint 2010.

 

See you,

Amadeu.

Advertisements

2 Responses to URL Rewriting – Part 2: Outbound Rule

  1. Pingback: URL Rewriting – Part 3: Integrating with SharePoint 2010 « 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: