IIS7 And URL Rewrite Reveals The Tricks Of Heavy Hitter Blog Sites

By alschneider at January 18, 2010 10:13
Filed Under: Marketing, Web / Software Development

Have you ever wondered how blog and other advertising sites create an unending stream of URL's that contain the title? For example, our blog uses a trick called URL Rewrite, an add-on for IIS7. With this tool, you use regular expressions to create a filter that parses the info for you and redirects it to another page that can use that data to fill in the blanks.

 

For example: You can have a bot / SEO friendly URL like

http://www.esourcedevelopment.com/BlogPosts/3548/Windows_7_upgrade_horrors

which will actually be rewritten as:

http://www.esourcedevelopment.com/ShowRealPost.aspx?id=3548?title=Windows_7_upgrade_horrors

The URL that IE and Firefox show in the browser address space is the same as the first one requested, but the page actually displayed is the second one.

 

Setting up IIS7 to handle URL Rewrite

  • Go to IIS Manager
  • Select “Default Web Site”
  • In the Feature View click “URL Rewrite“
  • In the “Actions” pane on right hand side click on “Add rules…”
  • In the "Add Rules" dialog, select the "Blank Rule" and click "Ok"

Now you must define the actual rewrite rule. In the URL rewrite module, a rewrite rule is defined by specifying four required pieces of information:

  • 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.

Naming a rule
In the “Name” text box enter a name that will uniquely identify the rule, for example: ”Rewrite to article.aspx”.

Defining a pattern
In the “Pattern” text box enter the following string:

^blogposts/([0-9]+)/([_0-9a-z-]+)

This string is a regular expression that specifies that the pattern will match any URL string that meets the following conditions:

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

Notice that certain parts of the regular expression are within parentheses. These parentheses create capture groups, which can be later referenced in the rule by using back-references.

Defining an action
Since the rule that we are creating is supposed to rewrite the URL, choose the “Rewrite” action type that is listed in the “Action” group box. In the “Rewrite URL:” text box, enter the following string:

ShowRealPost.aspx?id={R:1}&title={R:2}

 

This string specifies the new value to which the input URL should be rewritten. Notice that for the values of the query string parameters we used {R:1} and {R:2}, which are back-references to the capture groups that were defined in the rule pattern by using parentheses.

 

Now your actual page can decipher the QueryString values for ID and / or for title to search your database and display the article in question.

 

The entire tutorial on this setting can be found at http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/

 

This site features a lot of 'how-to' information on IIS and is invaluable for any web developer.

Comments

1/30/2010 2:26:39 PM #

craftsman sears

Thank nice post

craftsman sears United States | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




Looking for more stories? Check out
Shoutwire Shoutwire - Internet News for the Masses

Help us out by visiting our sponsors!
Thank you

Rifftrax


Recent Posts

Recent Comments

Comment RSS

What We're Playing




Who's Watchin' Me?