How to migrate to a new domain without breaking your Google search results

21 Mar
2009

Last week I moved my domain from http://www.wadolabs.com/blog to http://blog.wadolabs.com.  It was a simple migration as far as my application and data goes. But what about all the links and referring sites?  Plus the indexes that Google already had on my site.  I didn’t want all my pages to all of the sudden disappear and my visitors to run into a huge 404 error.  So I had a simple solution.  I created a new application under http://www.wadolabs.com/blog and only added a global.asax file to the application.  On the Application_BeginRequest() method, I added the following lines of code.

1
2
3
4
5
6
7
protected void Application_BeginRequest(object sender, EventArgs e)
{
    Response.Clear();
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", "http://blog.wadolabs.com");
    Response.End();
}

This redirects your site via a 301 HTTP response code to the your new site.  In my case http://blog.wadolabs.com .  This keeps all my current links active and avoided a big fat 404 error.  You can do more sophisticated logic here and check the requested page and map the request to the new location, but for me, I was okay just doing a global redirect to the root of my new domain.

Comment Form

top