No more 404 Not Found...?
Rather than giving each user a sorry "404 Not Found" apology page, how about trying to send them to a useful page? The following TrafficScript™ example shows you exactly how to do that, without having to modify any of your web site content or configuration. The TrafficScript rule works by inspecting the response from the webserver before it's sent back to the remote user. If the status code of the response is '404', the rule sends back a redirect to a higher level page:
Here is the code (it's a response rule):
if( http.getResponseCode() == 404 ) {
$path = http.getPath();
# If the home page gives a 404, nothing we can do!
if( $path == "/" ) http.redirect( "http://www.google.com/" );
if( string.endsWith( $path, "/" ) ) $path = string.drop( $path, 1 );
$i = string.findr( $path, "/" );
$path = string.substring( $path, 0, $i-1 )."/";
http.redirect( $path );
}
Your users will never get a 404 Not Found message for any web page on your site; ZXTM will try higher and higher pages until it finds one that exists. Of course, you could use a similar strategy for other application errors, such as 503 Too Busy. The same for images...This strategy works fine for web pages, but it's not appropriate for embedded content such as missing images, stylesheets or javascript files. For some content types, a 404 response is not user visible and is acceptable. For images, it certainly isn't. It will display a broken image icon, but a simple transparent GIF image would be more appropriate:
if( http.getResponseCode() == 404 ) {
$path = http.getPath();
# If the home page gives a 404, nothing we can do!
if( $path == "/" ) http.redirect( "http://www.google.com/" );
# Is it an image?
if( string.endsWith( $path, ".gif" ) || string.endsWith( $path, ".jpg" ) ||
string.endsWith( $path, ".png" ) ) {
# Hope that this image exists!
http.redirect( "/images/blank.gif" );
}
# Is it a stylesheet (.css) or javascript file (.js)?
if( string.endsWith( $path, ".css" ) || string.endsWith( $path, ".js" ) ) {
# Send the 404 message through
break;
}
if( string.endsWith( $path, "/" ) ) $path = string.drop( $path, 1 );
$i = string.findr( $path, "/" );
$path = string.substring( $path, 0, $i-1 )."/";
http.redirect( $path );
}
Obviously, you'll need to ensure that the link to the transparent ' We use a similar technique on support.zeus.com to catch and redirect old links as a result of a site reorganization a year or so ago...
Owen Garrett
[Zeus Dev Team] 08 February 2007
|
Recent Articles
Other Resources
|

When you move content around a web site, links break. Even if you've patched up all your internal links, site visitors from external links, outdated search results and people's bookmarks will be broken and return a '404 Not Found' error.
