vADC Docs

Simple web page changes - updating a copyright date

by on ‎02-21-2013 01:25 AM - edited on ‎07-07-2015 01:16 PM by PaulWallace (1,094 Views)

It's that time of the year, when the boss reminds you that you've got to change every single 'Copyright 2006' in the footer of your web pages to 'Copyright 2007'... at midnight, New Year's Eve.

 

Fear not! With a little TrafficScript, you can celebrate with everyone else and lay the guilt on the boss when you return in the New Year.  The trick is to add a TrafficScript response rule that rewrites all of your outgoing web pages, but only after midnight on January 1st:

 

# First, check the date   
if( sys.time.year() < 2007 ) break;  
     
# Now, check it's a web page  
$contenttype = http.getResponseHeader( "Content-Type" );  
if( ! string.startsWith( $contenttype, "text/html" ) ) break;  

 

The difficult bit is working out which bits of content to rewrite. You can't just change every 2006 to the new date, because there may be lots of dates in the web content that you don't want to change.

 

For my site, the footer of every page says:

 

Copyright MySite.com 1995-2006

 

... and it's in the last 250 bytes of the page (at least, before we insert our Tracking user activity with Google Analytics code).

 

The following code reads the entire response and rewrites it on the fly.  Note that 'http.getResponseBody()' deals with all of the awkward HTTP protocol parsing for you, decompressing compressed responses and reassembling chunked transfers for dynamic applications, so you don't need to downgrade the request to HTTP/1.0, disable keepalives, remove Accept-Encoding headers or anything else:

 

$body = http.getResponseBody();  
  
$start = string.drop( $body, 250 );  
$end = string.skip( $body, string.len( $body )-250 );  
  
$end = string.replace( $end, " 1995-2006", " 1995-" . sys.time.year() );  
  
http.setResponseBody( $start . $end );  

 

That's it - Happy New Year!

 

For reference, here's the entire rule:

 

# First, check the date   
if( sys.time.year() < 2007 ) break;  
     
# Now, check it's a web page  
$contenttype = http.getResponseHeader( "Content-Type" );  
if( ! string.startsWith( $contenttype, "text/html" ) ) break;  
  
$body = http.getResponseBody();  
  
$start = string.drop( $body, 250 );  
$end = string.skip( $body, string.len( $body )-250 );  
  
$end = string.replace( $end, " 1995-2006", " 1995-" . sys.time.year() );  
  
http.setResponseBody( $start . $end );  

 

This article was originally published 22 December 2006

Contributors