vADC Forum

New Contributor
Posts: 4
Registered: ‎03-04-2013
Accepted Solution

Best way to automate static redirects?

Hi All,

I'm looking for the best way to preserve static links from an 'old' site when the site moves to a new infrastructure behind a Riverbed Stingray using TrafficScript.

What we are trying to do is this:

$path = http.getpath();

if( http.getheader( "Host" ) == ""

        && string.contains( $path, "oldpath/faqs" ) ){

        http.changeSite( "" );


But we are trying to do it for 200+ differing links that may very well get updated frequently by our customer. The perfect scenario would be to have a traffic script rule reference a file that has <from link> TAB <to link> that we can upload via extra files as it is changed, and have the rule read the changes. The only other requirement is that the redirects have to be 302's ie permanent redirects.

Any suggestions guys?

Posts: 227
Registered: ‎11-29-2012

Re: Best way to automate static redirects?


     Firstly, welcome to Splash! 

Let me start at the "teach a man to fish" level and give you some building blocks to use in this case:

  1. TrafficScript can open a file located in the "Extra Files" catalog using resource.get() or resource.getlines().
  2. You can check if a file has been modified since it was last read using resource.getMD5() or resource.getMTime().
  3. You can build a hash of OldURL to NewURL mappings using the OldURL as the hash key and the NewURL as the value.  There is a good document here: HowTo: TrafficScript Arrays and Hashes that shows you how to use hashes.
  4. If you sort the keys of the hash in the right way, you can make searching through the hash much more effective.  Often times a reverse alphabetical sort on the has will match most specific to least specific so you can ensure that the list order doesn't effect your lookup. Look at

Remember that the TrafficScript reference is on every STM and is available from the TrafficScript editing page and has sample code for each of the functions I have listed above:

https://<YOUR STM IP OR HOSTNAME>:9090/apps/zxtm/help.fcgi?section=TrafficScript%20ReferenceGoogle Chrome071.png

Posts: 227
Registered: ‎11-29-2012

Re: Best way to automate static redirects?

Now that we have done the "Teach a Man to Fish" listing, here is a sample rule written by my esteemed colleague Michael Granzow that should do the trick.  I have pinged Michael offline as I believe there was something he wanted to tune on this rule, so he might jump in and make some modifications, but here is the rule here:

TrafficScript To Map Old URL's to New URLs

New Contributor
Posts: 4
Registered: ‎03-04-2013

Re: Best way to automate static redirects?

Thanks Aidan,

Ive tested the script and whilst it works as designed, what we are trying to do is subtly different.

The best way to describe the difference is to refer to the comments in the script

  1. # Element one is the string RD or RW depending if we are redirecting 
  2. #   the connection or rewriting it 
  3. # Element two is the old URL   
  4. # Element three is the new URL   
  5. # example:   
  6. # RD /oldurl   

Line 3, old URL, is specified as a /path not as a, which is what we require.

I was thinking one solution would be to add an extra column in the text file called host between the RW and the url ie row 2, and then search for both but Im not sure how this would affect the optimisation code.

Anyone suggest the best method to achieve this?

Frequent Contributor
Posts: 321
Registered: ‎11-29-2012

Re: Best way to automate static redirects?

Hi Adam,

You can modify the code in Michael Granzow's article TrafficScript To Map Old URL's to New URLs to cater for the file format you want. i.e. full URLs in the second column of the file.

Line 160 - where the code is: $path = http.getPath();  - extend that as follows:

# Set $path to be full URL e.g.

# Assumes that you are running http and https on the standard ports

if( ssl.isSSl() ) {

  $path = "https://" . http.getHostHeader() . http.getPath();

} else {

  $path = "http://" . http.getHostHeader() . http.getPath();


Then, a little further down, line 177 - where the code is http.setPath( $rwPath );,  change that to:

   if( string.regexMatch( $rwPath, "https?://([^/]*)(.*)" ) ) {

      http.setHeader( "Host", $1 );

      http.setPath( $2 );


   } else {

      log.warn( "checkPaths returned malformed URL: '".$rwPath."'" );


Then you can use a redirect file that contains full URLs like the following

RW http://stingray/foo http://stingray/bar

RW http://stingray/foo/bar http://stingray/baz

RW http://stingray/bar http://stingray/grub

RW http://stingray/1foo* http://stingray/bar

RW http://stingray/1foo/bar* http://stingray/baz

RW http://stingray/1bar* http://stingray/grub

RW http://stingray/1foo/* http://stingray/bar

RW http://stingray/1foo/bar/* http://stingray/baz

RW http://stingray/1bar/* http://stingray/grub

and Michael's rule works fine.

Best regards


New Contributor
Posts: 4
Registered: ‎03-04-2013

Re: Best way to automate static redirects?

Hi Owen, Aidan,  All,

I got this working, but I had to modify the way we processed a redirect, as follows:

Instead of line 177 - where the code is http.setPath( $rwPath );,  changed that to:

   http.setPath( "" );

   http.changeSite( $rwPath )

instead, and we are now working as expected!

Thanks all for your help!

Join the Community

Get quick and easy access to valuable resource designed to help you manage your Brocade Network.