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

About

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

Sponsors

Recent Tweets

  • @SaraJChipps StackOverflow has keyboard shortcuts? Is there a page that lists them out? 2 days ago
  • @shanselman I use keyboard shortcuts in gmail and reader. They're only useful on sites you visit frequently enough to bother learning them. 2 days ago
  • @jcroft 1 or 2 in general, but it's dependent on who my target audience is. 3 days ago
  • "Wide left". The two sweetest words in the English language. The Pats are going to the Super Bowl! 1 week ago
  • RIAA Reminds Us Why We Hate Them With Obnoxious Smartass Tweet http://t.co/SHhaijFC #fb 2 weeks ago