Forum Discussion
Using switch vs if to clean up irule
I'm attempting to clean up an irule that currently uses lots of "if" statements and want to use the "switch" statement instead. I've attempted to format the iRule as follows, but I cannot seem to get the syntax correct.
Any assistance is much appreciated!
when HTTP_REQUEST {
Redirect to www.site.org
switch -glob [string tolower [HTTP::host]] {
"site.org" -
"site.com" -
"www.site.com"
{
HTTP::respond 301 Location "http://www.site.org[HTTP::uri]"
}
Match uri patterns and redirect to https
switch -glob [string tolower [HTTP::uri]] {
"/uri1*" -
"/uri2*" -
"/uri3*" -
"/uri4*" -
"/uri5*"
{
log local0.alert "https redirect:[HTTP::host]-[HTTP::uri]-[HTTP::path]"
HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]"
return
}
Match uri's and send traffic to pool
log local0.alert "BEGIN Host:[HTTP::host]-[HTTP::uri]-[HTTP::path]"
switch -glob [string tolower [HTTP::uri]] {
"/" -
"/*" -
"/\\?*" -
"/uri10/*" -
"/uri11/*" -
"/uri12/*" -
"/uri13/*" -
"/uri14/*" -
"/uri15*"
{
log local0.alert "pool1 Host:[HTTP::host]-[HTTP::uri]"
pool pool1
return
}
default {
pool pool2
}
}
}
}
4 Replies
- Pconlan_71037
Altocumulus
Looks to me like you are missing a closing curly brace after each of the first two switch statements, and an extra one after the last switch statement. Does this work?
when HTTP_REQUEST {
Redirect to www.site.org
switch -glob [string tolower [HTTP::host]] {
"site.org" -
"site.com" -
"www.site.com"
{
HTTP::respond 301 Location "http://www.site.org[HTTP::uri]"
}
}
Match uri patterns and redirect to https
switch -glob [string tolower [HTTP::uri]] {
"/uri1*" -
"/uri2*" -
"/uri3*" -
"/uri4*" -
"/uri5*"
{
log local0.alert "https redirect:[HTTP::host]-[HTTP::uri]-[HTTP::path]"
HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]"
return
}
}
Match uri's and send traffic to pool
log local0.alert "BEGIN Host:[HTTP::host]-[HTTP::uri]-[HTTP::path]"
switch -glob [string tolower [HTTP::uri]] {
"/" -
"/*" -
"/\\?*" -
"/uri10/*" -
"/uri11/*" -
"/uri12/*" -
"/uri13/*" -
"/uri14/*" -
"/uri15*"
{
log local0.alert "pool1 Host:[HTTP::host]-[HTTP::uri]"
pool pool1
return
}
default {
pool pool2
}
}
} - rdessert_76127
Nimbostratus
That worked perfect! The rule saved successfully. I'm still working on getting proper syntax down :-)
Thanks for the help.
Rich - hoolio
Cirrostratus
You should also add a return after this line to ensure that you don't continue evaluating the rule code after sending a redirect:
HTTP::respond 301 Location "http://www.site.org[HTTP::uri]"
return
Aaron - rdessert_76127
Nimbostratus
Thanks Aaron!
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