Forum Discussion

Mike_Schnorr_64's avatar
Mike_Schnorr_64
Icon for Nimbostratus rankNimbostratus
Oct 05, 2007

Improve Big Redirect

I have a redirect rule that has grown into a 180 line case statement. It is getting to be a pain to maintain, so I'd like to put the rules for what redirects where into an data group. It is proving to be more difficult than I thought.

Current rule looks like:


switch -glob [string tolower [HTTP::host]] {
    "*domain1.com" { 
      HTTP::redirect "http://SomeURL1"
    }
    "*domain2.com" {
      HTTP::redirect "http://SomeURL2"
    }
    "*domain3.com" {
switch -glob [string tolower [HTTP::uri]] {
"/path1*" { 
  HTTP::redirect "http://SomeURL3"
}
"/path2*" { 
  HTTP::redirect "http://SomeURL4"
}
default {
HTTP::redirect "http://SomeURL5"
}
}
    }
}

Everything except the nested switch seems is fairly straight forward using this rule:


when HTTP_REQUEST {
regsub -nocase www. "[HTTP::host]" "" HostLessWWW
set redirectURL [findclass $HostLessWWW $::redirects " "]
HTTP::redirect "$redirectURL"
}

with this data group:


class redirects {
  "domain1.com http://SomeURL1"
  "domain2.com http://SomeURL2"
  "domain3.com http://SomeURL3"
}

I am stumped trying to expand it to take care of the logic in the nested case statement.

I thought of possibly looping over the findclass call while shrinking the URL from the right. i.e

first loop: domain3.com/dir1/dir2/file1.html

second loop: domain3.com/dir1/dir2

third loop: domain3.com/dir1

fourth loop: domain3.com

If at any point I get a match class I can stop and redirect to the url from findclass. This seems rather clunky and there should be a better way.

Anyone have any ideas?
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    That's the only approach I've been able to come up with so far.

     

     

    I just dug out some code for you & posted to the codeshare -- will save you quite a bit of work, I think:

     

    http://devcentral.f5.com/wiki/default.aspx/iRules/FindclassAgainstVariableLengthURI.html

     

    (Click here)

     

     

    HTH

     

    /deb