ASP.NET Developer. ALT.NET Supporter. Pragmatic Programmer. Published Writer.

Moving Domains Using ASP.NET

As some of you may have noticed, I recently moved this blog from "lypang.com/devblog" to "kevinwilliampang.com".  The motivation for this was purely cosmetic as I got tired of seeing "/devblog" in all my url paths.

Since the new site's structure is identical to the original site's structure, the migration only involved making the following change to the old site's global.asax file (if you're wondering why I broke up the url, it was to prevent the code syntax highlighter from interpretting the "//" in "http://" as the start of a comment :-P )

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string oldPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
    string newPathAndQuery = oldPathAndQuery.ToLower().Replace("/devblog", "");
    string url = "http:/" + "/www.kevinwilliampang.com" + newPathAndQuery;

    Response.Clear();
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", url);
    Response.End();
}

What this code does is trigger a 301 permanent redirect on any request for a page on the old lypang.com/devblog site.  It persists the requested path and query so that the redirection is seemless to the end user (e.g. a link to http://lypang.com/devblog/post/metrosexual-developers.aspx will 301 redirect to http://www.kevinwilliampang.com/post/metrosexual-developers.aspx). 

Obviously, for more complex sites and/or migrations this sledgehammer of a solution would not be sufficient.  In those instances, you would most likely be better off by redirecting to a static page and losing the path/query persistence or putting custom redirect logic on each individual page to ensure that they redirect properly. But for anyone who simply wants to move their website to another domain, this is a quick and easy way to do it.

Enjoyed this post? Share it with others!

  • DotNetKicks
  • DZone
  • Reddit
  • HackerNews
  • StumbleUpon
  • del.icio.us

2 Comments to Moving Domains Using ASP.NET

  1. July 13, 2008 at 12:01 am | Permalink

    Plus, everyone subscribed to your RSS feed gets all the recent posts showing up as new again, so that’s a big plus if you were trying to remind them of your website.

  2. July 13, 2008 at 12:04 am | Permalink

    Yeah, I noticed that too. Oops. :-P

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About

Kevin Pang is an ASP.NET developer and published writer with over 6 years of experience in the software industry.

Recent Tweets