Thursday, August 17, 2006

URL Rewriting using UrlRewritingNet.UrlRewrite

To rewrite URL's I am actually going to use a component from urlrewriting.net. You will need to download a sample project from their site in order to follow along. I will run through what you will need to copy from that project to use in your own projects. I'll also show you how I am using it in my projects.

In order to use url rewriting in your project copy UrlRewritingNet.UrlRewriter.dll from the sample's bin folder to your bin folder. Also copy the "xsd" folder to your project. If your project was created from the Atlas template or if you have modified your web.config file then you will have to copy some code out of the sample web.config file. Otherwise you will be okay to copying the sample web.config file to your project.

If you are modifying your web.config file:

  1. Find the configSections section. Copy the "section" tag from the sample to your configSections. If you don't have one just copy the entire configSections tag.
  2. Under the system.web section find httpModules and copy the "add" tag from the sample to your httpModules section. If you do not have these sections you can copy them from the sample.
  3. Copy the entire "urlrewritingnet" section from the sample to your web.config file. Delete everything that is within the "rewrites" section.
Now we will need to add our own custom rewrite tags. A big advantage of this component is that you can use regular expressions in your urls. This is what my rewrite tag looks like:
<add virtualUrl="^~/(.*)/(.*).aspx" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/Default.aspx?PageID=$1&Title=$2" ignoreCase="true"/>
The first wildcard I use as the Page ID. In my Default.aspx page I can then query my database and return some content based on whatever page I am on. The second wildcard can be whatever I want - I strictly use it for SEO purposes. Of couse you will have to add some error checking because your first wildcard might not be a Page ID, but you get the idea.

This component is free, easy to use, and I highly recommend it.

3 comments:

Anonymous said...

you have to replace the caracter & by & in asp.net 3.5

dhaker

Anonymous said...

& to "& a m p;" delete the space
dhaker

Anonymous said...

this code is working fine in localhost but not working when i am uploading it in my windows server, please help me asap.