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?

 

Published Sunday, August 27, 2006 10:15 AM by omar
Filed under:

Comments

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

Sunday, August 27, 2006 1:50 PM by .Net Adventures
In your code you forget about POST data ...

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

Friday, February 02, 2007 12:49 PM by Andrew Casad

Any suggestions on how to do this using PHP?

I am trying to accomplish the same thing, but using Apache and PHP.  Thanks!

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

Friday, March 28, 2008 12:02 PM by Ed Sansbury

This looks like a good idea which I can use. Thanks :)

# A few missing steps

Thursday, July 03, 2008 8:00 AM by Daishik Chauhan

Thanks for this article:

However, you missed a few steps:

1. If the site you are redirecting from is not your default site, you have to specify the same host header on the redirector site.  Then when you switch off the old site, the redirector will handle requests.

2. When you add the ISAPI filter in the configuration, there is a checkbox to 'Verify that file exists'.  YOU MUST UNCHECK THIS.  If you don't, the browser will search for the file in the redirector website and return a 404 error before the server can do the redirect.

Leave a Comment

(required) 
(required) 
(optional)
(required)