Redirect Traffic from old to new server when you change hosting service

How do you redirect your users when you change hosting service? Currently your domain maps to the IP which is with the current hosting provider. When you change to a new hosting provider, you get a new IP range for your new servers. So, even if you change the DNS mapping, it will take at least 4 days to get refreshed all over the world. So, during these 4 days, some user will go to new IP and some will go to old IP.

The problem here is redirecting all users to the new IP without letting themknow anything.

At Pageflakes, We have done this many times. We had all sorts of problem with our hosting providers and have changed servers almost once every month. So, we had to come up with a solution which works transparently and without any downtime.

Here’s what we do. First we map a new subdomain like new.pageflakes.com to the new server IP. Then we create a new web site (not virtual directory) on old web server called “Redirectors”. It maps to a folder which has nothing but a global.asax and web.config. Then we go to Web site Properties -> Home Directory -> Configuration and map ASP.net to receive all web requests. This includes all URLs including .html, .gif, .css. Js etc.

Next, we write the following code in Global.asax which redirects all traffic to the new server.


protected
void Application_BeginRequest(Object
sender, EventArgs e)
    {


string url =
HttpContext.Current.Request.Url.AbsolutePath;

string QueryParams =
HttpContext.Current.Request.QueryString.ToString();

if (QueryParams !=
"") {
          Response.Redirect(
"http://new.pageflakes.com" + url +
"?"+ QueryParams);
   }
else {
          Response.Redirect(
"http://new.pageflakes.com" + url );
   }
    }

So, anyone trying to go to www.pageflakes.com/aboutus.html gets redirected to new.pageflakes.com/aboutus.html. The redirector keeps the query string and logical path intact. So, complicated URLs like www.pageflakes.com/flakes/amit/notepad/notepad.html?haha=huhu&hehe=hoho gets converted nicely.

After doing this, we stop the default web site which is listening to port 80 and turn on the redirector. By this time, new server is already up and running on the new subdomain.

Now we do the DNS change and map the new server’s IP to www.

So, those users who still have the old IP in their DNS cache goes to the old server and then gets redirected to the new server. But after a while when their DNS cache gets refreshed and they get the new IP, all their requests go to the new server, so we have nothing to do here. After 4 or 5 days, we can safely wipe out the old server and start kicking old hosting providers backend.

Anyone has a better idea?

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

No related posts.

It is The God who erected the heavens without pillars that you [can] see; then He established Himself above the Throne and made subject the sun and the moon, each running [its course] for a specified term. He arranges [each] matter; He details the signs that you may, of the meeting with your Lord, be certain. (Quran 13:2)

8 Comments

Leave a Reply

 

Get Adobe Flash playerPlugin by wpburn.com wordpress themes