vADC Docs

No more 404 Not Found...?

by on ‎02-21-2013 05:19 AM - edited on ‎06-11-2015 03:23 PM by PaulWallace (2,404 Views)

ft-sm-404.pngWhen 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.

 

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:

 

http://www.site.com/products/does/not/exist.html returns 404, so try:

http://www.site.com/products/does/not/ returns 404, so try:

http://www.site.com/products/does/ returns 404, so try:

http://www.site.com/products/ which works fine!

 

 

Here is the code (it's a Stingray 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; Stingray 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 may not be. Some browsers will display a broken image icon, where 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" ) ) {  
           http.sendResponse( "200 OK", "image/gif",  
              "GIF89a\x01\x00\x01\x00\x80\xff\x00\xff\xff\xff\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3b",  
              "" );  
      
       }  
      
       # Is it a stylesheet (.css) or javascript file (.js)?  
       if( string.endsWith( $path, ".css" ) || string.endsWith( $path, ".js" ) ) {  
         http.sendResponse( "404", "text/plain", "", "" );  
          break;  
       }  
      
       if( string.endsWith( $path, "/" ) ) $path = string.drop( $path, 1 );  
      
       $i = string.findr( $path, "/" );  
       $path = string.substring( $path, 0, $i-1 )."/";  
      
       http.redirect( $path );  
    }  

 

Contributors