Forum Discussion
Need help making Irule more efficient
Below is a sample of what is running. Each country/culture has two seperate if statements because we don't want to disrupt flow to other items in those folders. We are in the middle of switching from our old CMS to a new one and need to keep parts of both CMSes alive for now so we are usign iRules to route for 20 different country home pages on 3 different server pools. We have mostly used "equals" but there are one of two instances of "contains" mixed in.
Any advice on how to improve the structure and perfomance would be appreciated.
FYI I am a Sys Admin not a programmer.
Thanks in advance.
John
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] equals "/" }{
HTTP::redirect "http://mysite.com/home/"
}
if { [string tolower [HTTP::uri]] equals "/gb/eng/default.asp" }{
HTTP::redirect "http://[HTTP::host]/home/en-gb/"
}
if { [string tolower [HTTP::uri]] equals "/gb/eng/" }{
HTTP::redirect "http://[HTTP::host]/home/en-gb/"
}
if { [string tolower [HTTP::uri]] equals "/fr/fra/default.asp" }{
HTTP::redirect "http://[HTTP::host]/home/fr-fr/"
}
if { [string tolower [HTTP::uri]] equals "/fr/fra/" }{
HTTP::redirect "http://[HTTP::host]/home/fr-fr/"
}
if { [string tolower [HTTP::uri]] contains "/home/admin-tools/" }{
HTTP::redirect "http://[HTTP::host]/home/"
}
}
2 Replies
- Kevin_Stewart
Employee
At a minimum you should probably use an if/elseif structure to avoid unnecessary evaluations. Also, for everything except your single "contains" conditional, you could use a switch context, which is generally faster.when HTTP_REQUEST { if { [string tolower [HTTP::uri]] contains "/home/admin-tools/" } { HTTP::redirect "http://[HTTP::host]/home/" } else { switch [string tolower [HTTP::uri]] { "/" { HTTP::redirect "http://mysite.com/home/" } "/gb/eng/default.asp" - "/gb/eng/" { HTTP::redirect "http://[HTTP::host]/en-gb/" } "/fr/fra/default.asp" - "/fr/fra/" { HTTP::redirect "http://[HTTP::host]/fr-fr/" } } } }
Given that you have 20 countries to check for, and presumably 40 conditions, it may also make sense to throw everything into a data group for easier management:
Example data group (my_country_datagroup)
"/" := "/home/"
"/gb/eng/default.asp" := "/en-gb/"
"/gb/eng/" := "/en-gb/"
"/fr/fra/default.asp" := "/fr-fr/"
"/fr/fra/" := "/fr-fr/"
Example iRule:
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] contains "/home/admin-tools/" } {
HTTP::redirect "http://[HTTP::host]/home/"
} else {
if { [class match [string tolower [HTTP::uri]] equals my_country_datagroup] } {
HTTP::redirect "http://[HTTP::host][class match -value [string tolower [HTTP::uri]] equals my_country_datagroup]"
}
}
} - jcline
Nimbostratus
Thanks!
That is exactly what I was looking for. I just didn't know how to put it together.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com