Forum Discussion

Jeremyah_Corner's avatar
Jeremyah_Corner
Icon for Nimbostratus rankNimbostratus
Apr 03, 2006

Big Redirects List

 

I've got a lengthy list of redirs that I want to use F5 to do the work with, instead of a custom ASP solution that is running currently.

 

 

The basic gist..

 

 

I've got a list:

 

 

site1 site1a

 

site2 site1a

 

site3 site1a

 

site4 site2a

 

site5 site2b

 

site6 site3b\widget.htm

 

site7 site2b\widget.asp

 

 

etc...

 

 

I was reading and I noticed a method that might be useful, such as building a class that contains this list and using a find or matchclass to compare the URI to that list and extracting the second element of that match as a http::redirect "Result"

 

 

Of course, I understand the basics of if, else, ::uri and ::redirect, what I'm missing is clues on extracting the matched pair from the class list as the element to give to the redirect string.

 

 

-- part 2, wild card redirects

 

In addition to having specificly site1.site.com and wanting it to go to site1a.othersite.com if all else fails in this long list I want anything .site.com to go to othersite.com\search.asp.. could this be included in the class list (at the end) and it function approprately?

6 Replies

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    To search a class and retrieve the second part of the class member, you can use findclass:

     

     

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

     

     

     

    findclass [(separator)]

     

     

    * Searches a data group list for a member that starts with a specified string and returns the data-group member string.

     

     

     

    As for the wildcard redirect, I would think you could just use an else at the end of the rule to perform a default action if the request didn't match a member of the class.

     

     

    Aaron
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Hoolio's right on on this one.

    The findclass command lets you specify a seperator and will return the resultant second half of the string in the class.

    For instance, the class in question:

    
    class myRedirects {
      "site1 site1a"
      "site2 site1a"
      "site3 site1a"
      "site4 site2a"
      "site5 site2b"
      "site6 site3b\widget.htm"
      "site7 site2b\widget.asp"
    }

    combined with a findclass statement such as:

    
    [findclass [HTTP::host] $::myRedirects " "]

    Would return site1a or site2a, etc. depending on which hostname was used. This is, of course, assuming that "site1" is the entire hostname of the site. If you're trying to use just the first portion, the canonical name for instance, then you'd want to make use of the getfield command, as well.

    That would look something like this:

    
    [findclass [getfield [HTTP::host] "." 1] $::myRedirects " "]   

    To put that into the context of your rule, it'd look like:

    
    when HTTP_REQUEST {
      set newDom [findclass [getfield [HTTP::host] "." 1] $::myRedirects " "]
      if { [string length $newDom] >= 1 } {
        HTTP::redirect $newDom
      } else {
        HTTP::redirect "http://othersite.com/search.asp"
      }
    }

    Hopefully that at least gets you pointed in the right direction.

    -Colin
  • So, I've built two lists, a redirects, and a search list.. I'm asking find the pair based on the host address from the myRedirects list, then redirect, else (if the length of newdom is less than or equal to 1) find the third field of the host name (Based on the .) and find it in mySearchs lists then redirect to somesite in that list, else do a base redirect to a global search page.

    Does this look right?

    class myRedirects {
      "site1.site.com site1a.site.com"
      "site2.site.com site2.othersite.com"
      "site3.othersite.com site1.site.com"
      "site4.site.com site5.othersite.com\boo.asp"
      "site3.site.com site5.othersite.com\woo.asp"
    }
    class mySearchs {
       "uk site.co.uk/search.asp"
       "jp site.co.jp/search.asp"
       "tw site.com.tw/search.asp"
    }
    when HTTP_REQUEST {
      set newDom [findclass [HTTP::host] $::myRedirects " "]
      if { [string length $newDom] >= 1} {
        HTTP::redirect $newDom
      } else {
      set myCatch [findclass [getfield HTTP::host] "." 3] $::MySearchs " "]
        if { [string length $myCatch] >= 1} {
         HTTP::redirect $myCatch
      } else {
         HTTP:redirect "http://mysearch.com"
      }
      }
    }

    Oh, and another condition, since I've got a healthy ammount of www. CNAMEs, I'd like to exlude them from the list and just pick up the relivant part behind it..

    when someone types in www.site1.site.com and it's CNAME'd the host still shows as www.site1.site.com and wouldn't match in this scenario, yes? How do I get around that? Woudl 'ends_with' be applicable in this 'host' scenario?
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    The rule looks pretty close. I'd make sure you have an open bracket before HTTP::host in your getfield command, though. Like this:

    
    set myCatch [findclass [getfield [HTTP::host] "." 3] $::MySearchs " "]

    As far as ignoring the www goes, yes, you could use ends_with to ignore the first portion of the hostname. As long as you're using the HTTP::host variable in the comparison, this should work fine.

    Colin