Want to show your appreciation? Please to my charity.

Saturday, October 09, 2010

IIS Web Site Redirect with Announcement Page

It is common that when you have moved your site from one host to another and you want to redirect all the traffic going to old host to be redirect to the new host before the old host is completely unavailable.

This can be easily done with any major web server. For IIS, Microsoft has a page about this and all you need to do is follow the instruction in the section of “to redirect requests to another Web site or directory”.

Redirect requests to another Web site or directory

Sweet, now you type in a URL going to your old site and you automatically landed to the correct page on your new site. But wait, what’s going to happen if one day you have to shutdown the old site completely. Your user’s bookmark and external link from other site will still be pointing to your old site because the redirect was so transparent that they would have never notice the need to correct their bookmark. When the day comes, they’ll suddenly end up with a broken error page.

Redirect all requests to a single file

Yes, we want to put up a page to let our user know about it so they can update their bookmark during the transition period when both old and new site are up and running. Fine, let’s use the method mentioned in the section “to redirect all requests to a single file”. Now our user can see the nicely written notice that the site was moved to a new home. But wait again, what should use do next after reading our notice, they want to go to that particular page on your site! Where should they click?

Redirect requests to a program

Well, according to Microsoft, we still have last option. That is redirecting to a program that is smart enough to tell user about the move and then send the user to the right page!? This sounds like what we wanted but does it really justify to write a binary executable for such a simple task? No, there got to be a simpler and better way!

Redirect to a smart HTML page

The solution is not difficult. We’re going to tweak the “redirect to program” method by creating an html page with Javascript instead of an binary executable. Let’s call the page “move.html”, you can host the move at anywhere, a virtual dir under old site, anywhere in new site, or a third site.

First, configure you old site’s properties to redirect the site to exactly “http://server/move.html?$P&URI=” without quotes. Make sure none of the checkbox options in the redirect to is checked. See the screenshot below:

image

Then create the move.html page with below content:

<html>
<script type="text/javascript">
var hu = window.location.search;
var position = hu.lastIndexOf('&URI=');
var query = hu.substring(0, position);
var path = hu.substring(position+5);
if (query == '?') query = '';
var url = 'http://thenewcoolsite' + path + query;
linkstring = '<a href="' + url + '">' + url + '</a>';
</script>
<title>The Cool Site Moved</title>
<body>
<h1>The Cool Site Moved</h1>
Please always use the <a href="http://thenewcoolsite/">http://thenewcoolsite/</a> 
to access the the cool site.
<p>
You just landed on the old host of the cool site. This host will be taken offline 
soon and you will get an error instead of this page when it happens. Please take
one of the actions below:
<ul>
<li>
If you access from your web browswer's bookmark or faviorate, please update it 
now with the new link below.
<li>
If you clicked on a link from an email, please notify the sender to correct the link.
<li>
If you are brought to this page by an application or another web site, please notiffy
the owner of the application ASAP.
</ul>
<p>
The new link to your request is in below. Please click on the link, it will take you
to the page that you are looking for.
<p>
<script type="text/javascript">document.write(linkstring);</script>
</body>
</html>

Now if user access http://oldcoolsite/somedeeplylinked/pagewithparameter.aspx?p1=abc&p2=789, you should get the page below with messages that you want to give to user, as well as a link to the new site that that the user attempted to access.

image

Limitations and Enhancements

For this to work, your user’s browser must support Javascript. This is the only limitation but can be a serious limitation for some web sites. But the logic in the Javascript can easily moved to server side with an ASP or JSP.

No comments:

Post a Comment