Simple ASP.NET 301 Moved Permanently Redirector
Posted by Dave Bouwman | Posted in ASP.NET, Blogging | Posted on 25-05-2009
0
I’ve been running DasBlog since I started blogging back in February 2005, and it had been good, but the time came to move on to something that required less hand-on work to keep current. Enter WordPress. You can read how the content migration went in a previoud post, but this is just about the final step in the process – setting up a redirector.
Armed with my csv file listing the old dasblog based urls and the new wordpress urls, I needed to build a simple ASP.NET app I could drop in the old location, and have it spit back “301 Permanently Moved” responses when someone (or Google) hits the old Urls. It’s important to let Google know that your pages have moved, thus we send a 301 server response, which tells the search engine to never come back to that original location.
I should note that I can do this because my blog’s address changed – from blog.davebouwman.net to blog.davebouwman.com – thus I still had the old location available to run a .NET application, which could do the re-direct. If you are paving over a .NET blog with WordPress, and hosting it on the same Url, you’ll have to find a different option.
What I came up with was a simple Global.asax that seems to work pretty well. Not sure how it would scale if I had 10,000 posts, but at 390, it’s just fine – besides it was the only part of this project that went as predicted. The code below is everything – I do have a simple empty Default.aspx page, and of course the linkmap.csv file, but that’s it. You can try it by following this link:
http://blog.davebouwman.net/2009/01/06/CodeCoverageWhatsEnough.aspx
<%@ Application Language="C#" %> <%@ Import Namespace="System.IO" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { //Load the CSV list into a hashtable and cache it LoadUrlsIntoCache(); } private void LoadUrlsIntoCache() { Hashtable urls = new Hashtable(); FileStream fs = File.OpenRead(Server.MapPath("./linkmap.csv")); using (StreamReader sr = new StreamReader(fs)) { while (sr.Peek() >= 0) { string line = sr.ReadLine(); if(line.Contains(",")) { string[] parts = line.Split(','); urls.Add(parts[0], parts[1]); } } } Context.Cache.Add("UrlCache", urls, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 20, 0, 0), CacheItemPriority.High, null); } protected void Application_BeginRequest(Object sender, EventArgs e) { string requestPath = HttpContext.Current.Request.Url.ToString(); Hashtable urls = (Hashtable)Context.Cache.Get("UrlCache"); if (urls == null) { LoadUrlsIntoCache(); urls = (Hashtable)Context.Cache.Get("UrlCache"); } string newUrl = ""; if (urls.ContainsKey(requestPath)) { newUrl = (string)urls[requestPath]; } else { //Requested Url not in cache... send to "missing content" page... newUrl = "http://blog.davebouwman.com/index.php/content-missing/?foo=" +requestPath; } Response.Clear(); Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", newUrl); Response.End(); } </script>

