Forum Discussion

gbunting's avatar
gbunting
Icon for Nimbostratus rankNimbostratus
Mar 19, 2009

How to clean up multiple if/else redirects?

I have a current iRule that works. It will redirect from www.domainname.com.au to www.domainname.com/au for multiple international domains. The problem is I currently have 7 if/else statements for 7 international domains and I have just found out that I will need to add approximately 20 more domains. My question is, is there a better way to create this rule that will be more easily manageable given the number of if/els statements I will need to add? I tried looking into using data groups, but I am not sure how to go about parsing them.

 

 

Here is my current iRule:

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] starts_with "www.osisoft.com"} {

 

pool www.osisoft.com

 

}

 

elseif { [HTTP::host] starts_with "www.osisoft.com.br"} {

 

HTTP::redirect "http://www.osisoft.com/Brazil"

 

}

 

elseif { [HTTP::host] starts_with "www.osisoft.com.au"} {

 

HTTP::redirect "http://www.osisoft.com/Australia"

 

}

 

elseif { [HTTP::host] starts_with "www.osisoft.com.mx"} {

 

HTTP::redirect "http://www.osisoft.com/Mexico"

 

}

 

elseif { [HTTP::host] starts_with "www.osisoft.com.ca"} {

 

HTTP::redirect "http://www.osisoft.com/Canada"

 

}

 

elseif { [HTTP::host] starts_with "www.osisoft.cz"} {

 

HTTP::redirect "http://www.osisoft.com/Chech"

 

}

 

elseif { ([HTTP::host] starts_with "www.osisoft.co.jp") ||

 

([HTTP::host] starts_with "www.osisoft.jp") } {

 

HTTP::redirect "http://www.osisoft.com/Japan"

 

}

 

else { pool www.osisoft.com

 

}

 

}

 

 

Thanks

 

 

Glen

3 Replies

  • Hi Glen,

     

     

    A switch (Click here) statement would be faster.

     

     

    Else, you can use a datagroup and the findclass command (Click here) to do parse the class.

     

     

    Aaron
  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus
    Have you considered using "switch" instead of if-then?

     

     

    You could use a Data List, but you'd have to apply a few tricks to get the functionality of data pairs. For maximum performance, F5's recommendation is to use a Data List when you have more than 100 entries.

     

     

    By the way, it looks like your iRule will not work for users who don't use "www." in the address.

     

     

    Also, you're using 302 redirects, which is a temporary redirect. I'd recommend using a 301 (permanent redirect) - you can use HTTP::respond to achieve this.

     

     

    Hope this helps.
  • I have changed the Irule to use the switch statement as changed the redirect to the 301 respond. Is there any other optimizations I should do? The majority of the traffic will be going to the default pool (www.domainname.com) can this be optimized by adding that to the beginning of the switch statement or is that not necessary?

     

    when HTTP_REQUEST {

     

    log local0. "in HTTP_REQUEST"

     

    switch [HTTP::host] {

     

    www.domainname.com.br {HTTP::respond 301 Location "http://www.domainname.com/Brazil" }

     

    www.domainname.com.au {HTTP::respond 301 Location "http://www.domainname.com/Australia" }

     

    www.domainname.com.mx {HTTP::respond 301 Location "http://www.domainname.com/Mexico" }

     

    www.domainname.com.ca {HTTP::respond 301 Location "http://www.domainname.com/Canada" }

     

    www.domainname.cz {HTTP::respond 301 Location "http://www.domainname.com/Chech" }

     

    www.domainname.co.jp {HTTP::respond 301 Location "http://www.domainname.com/Japan" }

     

    www.domainname.jp {HTTP::respond 301 Location "http://www.domainname.com/Japan" }

     

    default { pool www.domainname.com }

     

    }

     

    }

     

    Thanks,

     

    Glen