|As I've mentioned here, by the end of August 2000 the time had come to move my ASP examples from my personal home page at ShawThing.com to this new domain - CoverYourASP.com.|
This article describes the simple measures I took to ensure that people were seamlessly redirected, and even welcomed on the new site!
The first decision I made was to use my 404 handler to trap the pages that had been moved. I could have replaced the moved files with ones containing a redirect, but I decided not to - I hate duplicate code. If I had made a mistake in the redirect code I would have to change it again in each file.
So instead I set up my 404 handler to redirect. The code I used is shown below:
// handle all HTTP 404 errors (File not found)
function Handle404 ( )
// get the URL that failed - the syntax is "/404http//wwwShawThingcom/missingfile.htm"
// or, on my machine, "/404http//localhost/missingfile.htm"
var sMissingPath = "" + Request.QueryString ( );
// strip off everything before the last /
var nLastSlash = sMissingPath.lastIndexOf ( "/" );
var sMissingFile = sMissingPath.slice ( nLastSlash + 1 );
// if its a file that moved to CoverYourASP, redirect quietly
// make lowercase first
sMissingFile = sMissingFile.toLowerCase ( );
// split into filename and extension
var sParts = sMissingFile.split ( "." );
// the new filename on CoverYourASP
switch ( sParts  )
// these files were renamed during the move...
sNew = "/AboutASP.html";
sNew = "/default.html";
sNew = "/Reources.html";
// these files kept the same name
sNew = sMissingFile;
// am I going to CoverYourASP?
if ( sNew != undefined )
Response.Redirect ( 'http://CoverYourASP.com/' + sNew + '?FromShawThing' );
// display a missing file page
MissingFile ( sMissingFile );
The first problem I found with this is that people were linking straight to the ShowSource.asp page - this uses the QueryString to name the page to display. A typical URL would be ShowSource.asp?page=Categories. The 404 handler unfortunately stripped off the QueryString when it failed to find the file, so the user got redirected to ShowSource.asp - without the "page=Categories".
The solution was to leave a simple ShowSource.asp on my old site. This was the only page that used QueryStrings so I wasn't going against my "no duplicate code" principle. The file is shown below:
<% Response.Redirect ( 'http://CoverYourASP.com/ShowSource.asp?' + Request.QueryString ( ) + '&FromShawThing' ) ; %>
So, that's the redirects taken care of - everyone who visits an old page on ShawThing.com will end up at CoverYourASP.com. Now look again at the code above. Can you see the extra FromShawThing parameter I added to the URL? This is how I implemented the "welcome" text that appears at the top of the pages. If you want to see it, reload the page.
The welcome text was added to the Header () function you should all be familiar with. If not, you should read my article about how I use SSI first. The new code is shown below:
|// now I'm going to test if I've been redirected from ShawThing.com
// and if so point them at the article explaining what happened!
bRefered = Request.QueryString ( "FromShawThing" ).Count;
if ( bRefered )
Out ( '<td colspan=3 align="center"><a href="/HowIMoved.html">You have been redirected to CoverYourASP.com - find out why and how!</a></td></tr><tr>' );
You can see how I set a global variable "bRefered" if there is a "FromShawThing" in the QueryString. This variable can be used in the main page content if necessary. Incidentally, I could have checked for ShawThing in the Request.ServerVariables ( "HTTP_REFERER" ), but that's a more "expensive" call, and I couldn't then have demonstrated it above!