in Search

Blog Day Afternoon

Take a walk on the mild side.

URL Rewriting using Intelligencia UrlRewriter Example 1

Search engine friendly URLs have become more important. It's not very useful to have all of your store products, for example, have the same URL with the only difference being in the querystring. A URL such as http://www.storename.com/product.aspx?cat=123&prod=456 is not very intuitive. My favorite tool for creating search engine friendly URLs is Intelligencia's URLRewriter.dll. It is very easy to install in an ASP.Net website. After copying the Intelligencia.UrlRewriter.dll into the bin folder, just a few changes to the web.config file is needed. This tool came in very handy for our BestBrandsOn.TV website.

In <configSections>,  add <section name="rewriter" requirePermission="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter"/>.

In <httpHandlers>, add <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter"/>.

Now all that is left is for the entries to handle the particular re-writes needed in the website. These go into a new section <rewriter></rewriter> within the <system.web> section.

The BestBrandsOn.TV website has numerous categories of products such as Automative, Household, and Kitchen. I wanted to be able to list all products within a category using a URL of http://www.BestBrandsOn.TV/Automotive-Products.aspx or http://www.BestBrandsOn.TV/Household-Products.aspx. The general format of the URL is http://www.BestBrandsOn.TV/CategoryNameHere-Products.aspx. I added the following line in the <rewriter> section of web.config:

<rewrite url="~/(.+)-Products.aspx" to="~/ProductList.aspx?Cat=$1" processing="stop"/>

The URL of  http://www.BestBrandsOn.TV/CategoryNameHere-Products.aspx is rewritten to be http://www.BestBrandsOn.TV/ProductList.aspx?Cat=CategoryNameHere.

I also wanted unique URLs for each of the products, again using words in the URL that made sense. For example, http://www.bestbrandson.tv/Household/Shamwow.aspx clearly shows the category name of Household and the product name of Shamwow. The rewrite needed for URLs of this nature required the following entry in the <rewriter> section:

<rewrite url="~/Household/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>

The general format of a URL like http://www.BestBrandsOn.TV/CategoryNameHere/ProductNameHere.aspx is rewritten to be http://www.BestBrandsOn.TV/Category/Product.aspx?Prod=ProductNameHere. I added a line in the web.config for each category.

<rewrite url="~/Automotive/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
<rewrite url="~/Electronics/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
<rewrite url="~/Entertainment/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
<rewrite url="~/Health-Beauty/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
<rewrite url="~/Household/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
<rewrite url="~/Kitchen/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>
<rewrite url="~/Fitness/(.+).aspx" to="~/Category/Product.aspx?Prod=$1" processing="stop"/>

You could make an even more generic rewriting rule. With this setup, I have to add a line in the web.config file every time we add a new category. I chose to do it this way, however, because we have quite a few real subdirectories and I didn't want to put in a lot of <unless> entries to exclude the real subdirectories for URL rewriting. Click here to see an example of a website with a more generalized entry where I did choose to use <unless> entries.

Published Dec 18 2008, 10:22 AM by Blogette
Filed under: ,
Add to Bloglines Add to Del.icio.us Add to digg Add to Facebook Add to Google Bookmarks Add to Newsvine Add to reddit Add to Stumble Upon Add to Shoutwire Add to Squidoo Add to Technorati Add to Yahoo My Web