imageThis blog post is about how I used URL rewriting for a better organic search ranking. Maybe it helps out some other people who struggle to rewrite URL’s without components like URL Rewriter. It’s open source but it uses regular expressions in a configuration file. That is a more common way to rewrite, but that would make me rewrite a lot of code, because of the URL’s I have at the moment. And since developers are lazy efficient, we tend to try an other approach first.

My current URL’s are:



Product id 1 can be a car like Audi R8 and ID 2 can be a book like Harry Potter. Other solutions are based on URL’s like:


Which can be rewritten easily with a regular expression and the open source rewriting mentioned before. Here is how I solved it for my situation:


My project was recently auto migrated to asp.net 4.0 by opening the solution file in Visual Studio 2010. Which worked perfectly by the way. The Web server which has to run the project was Windows Server 2008 R2. because it was the R2 it contained IIS7.5

The first thing I had to do was installing the Microsoft web platform installer on the Web server

After that I could install the URL Rewrite module for IIS. it’s free and available on IIS.net (really recommended download!):


URL rewrite option in IIS

When I installed the same module on IIS7.0 (instead of 7.5) it somehow didn’t had as much preset rule templates. But on 7.5 I had the following:

Rule template of the URL rewrite component in IIS

I used the append or remove trailing slash to get this URL:

http://www.mydomain.com to rewrite to http://www.mydomain.com/

The next was cannibalize the domain name so that:

http://mydomain.com rewrites to http://www.mydomain.com

After doing that the only thing left was the part that I started this blog with: rewriting the product_details.aspx?id=1 to http://www.mydomain.com/the-fat-Audi-R8/ or the http://www.mydomain.com/harry-potter-book-1/

Therefore you need to map IIS requests to the asp.net worker process. otherwise IIS would handle the requests and now it will send the request to the asp.net process to handle it.

you have to open the web.config file and set:

<modules runAllManagedModulesForAllRequests="true" />


So after setting that web.config so that asp.net will handle the http requests you can start to make your project rewrite the URL’s

You need to start in the Global.asax file.

void Application_BeginRequest(object sender, EventArgs e) 
    string pathAndQuery = Request.Url.PathAndQuery.ToString().ToLower();

    if (!pathAndQuery.Contains(".")) // it's extensionless remember ;)
        Hashtable htSite = Util.GetHashTable();
        pathAndQuery = System.Web.HttpUtility.UrlDecode(pathAndQuery); // for german things which have umlauts in it
        if (htSite[pathAndQuery] != null)
            HttpContext.Current.RewritePath(htSite[pathAndQuery].ToString(), false); // if you leave out the optional boolean, you will break your .net theme's

In my Util class, I have this GetHashTable method which get’s stuff from the database and stores it in the Hashtable which I add to the Cache after it’s generated and filled.

public static class Util
    public static Hashtable GetHashTable()
        Hashtable ht = (Hashtable)System.Web.HttpRuntime.Cache.Get("hturlrewrite");

        if (ht == null)
            ht = new Hashtable();
            DataTable dt = GetYourUrlsMethod(); // your version

            foreach (DataRow dr in dt.Rows)
                string value = "/Category.aspx?catg=" + dr["CategoryGroupID"];
                string categoryName = dr["CategoryGroupName"].ToString();
                categoryName = categoryName.Replace(" ", "-").ToLower(); // a dash is better for SEO then an underscore and a plus is not allowed by default in IIS

                string key = "/" + categoryName + "/";
                    ht.Add(key, value);
            System.Web.HttpRuntime.Cache.Add("hturlrewrite", ht, null, System.DateTime.Now.AddHours(24), System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);  
        return ht;

Please do not forget to Cache this. because otherwise it would fire for each page request and access the database for building the Hashtable.

Well that is it for my implementation of URL rewriting in Asp.net 4.0 with IIS7.x with custom rewriting logic.

Good luck!

Edit: I recently encountered invalid viewstate errors. Here is how I fixed it: http://stackoverflow.com/questions/3854999/invalid-viewstate-when-using-rewritepath

kick it on DotNetKicks.com Shout it

Pin on pinterest Plus on Googleplus Post on LinkedIn