Forum Discussion
Help with irule using switch
- Jun 04, 2023
veredgfbll I believe what you're looking for is the following. Please keep in mind that you do not have to have the final else or the default action in the switch statement and these are just options if by chance you do have a default action you want to perform for the if statement if it doesn't match or the URI path for the single host match.
when HTTP_REQUEST priority 500 { set URI [string tolower [HTTP::uri]] if { [HTTP::host] == "xxx.com" } { switch -- ${URI} { "/yyy" { # can change http to https if you need to redirect to HTTPS instead HTTP::redirect "http://xxy.com/" } "uri_2" { # URI 2 action to execute } "uri_3" { # URI 3 action to execute } "uri_4" { # URI 4 action to execute } "uri_5" { # URI 5 action to execute } "uri_6" { # URI 6 action to execute } default { # default action to perform if URI doesn't match } } } else { # action to perform if HTTP::host doesn't match } }
- Jun 05, 2023
veredgfbll The iRule you created can be configured as the following which you will notice the "default" match is completely removed since you don't want to perform any other action other than what the iRule and virtual server will already do if a match isn't found.
when HTTP_REQUEST priority 500 { set URI [string tolower [HTTP::uri]] if { [HTTP::host] == "www.hostname.com" } { switch -- ${URI} { "/aaa" { HTTP::respond 302 Location "https://www.test.com/aaa" #log local0. "redirect to https://www.test.com2/aaa completed" } "/aab" { HTTP::respond 302 Location "https://www.test.com/aab" #log local0. "redirect to https://www.test.com2/aab completed" } "/aac" { HTTP::respond 302 Location "https://www.test.com/aac" #log local0. "redirect to https://www.test.com2/aac completed" } "/aad" { HTTP::respond 302 Location "https://www.test.com/aad" #log local0. "redirect to https://www.test.com2/aad completed" } "/aae" { HTTP::respond 302 Location "https://www.test.com/aae" #log local0. "redirect to https://www.test.com2/aae completed" } "/aaf" { HTTP::respond 302 Location "https://www.test.com/aaf" #log local0. "redirect to https://www.test.com2/aaf completed" } "/aag" { HTTP::respond 302 Location "https://www.test.com/aag" #log local0. "redirect to https://www.test.com2/aag completed" } } } }
veredgfbll I believe what you're looking for is the following. Please keep in mind that you do not have to have the final else or the default action in the switch statement and these are just options if by chance you do have a default action you want to perform for the if statement if it doesn't match or the URI path for the single host match.
when HTTP_REQUEST priority 500 {
set URI [string tolower [HTTP::uri]]
if { [HTTP::host] == "xxx.com" } {
switch -- ${URI} {
"/yyy" {
# can change http to https if you need to redirect to HTTPS instead
HTTP::redirect "http://xxy.com/"
}
"uri_2" {
# URI 2 action to execute
}
"uri_3" {
# URI 3 action to execute
}
"uri_4" {
# URI 4 action to execute
}
"uri_5" {
# URI 5 action to execute
}
"uri_6" {
# URI 6 action to execute
}
default {
# default action to perform if URI doesn't match
}
}
} else {
# action to perform if HTTP::host doesn't match
}
}
- veredgfbllJun 05, 2023Cirrus
Hi, This seems good - can I use the "return" command as the default option? (I'd rather have an option to continue as normal)... not quite sure what the default means here... is it like the else option?
Like this?
when HTTP_REQUEST { if {[HTTP::has_responded]} { return } set URI [string tolower [HTTP::uri]] if { [HTTP::host] == "www.hostname.com" } { switch -- ${URI} { "/aaa" { HTTP::respond 302 Location "https://www.test.com/aaa" #log local0. "redirect to https://www.test.com2/aaa completed" } "/aab" { HTTP::respond 302 Location "https://www.test.com/aab" #log local0. "redirect to https://www.test.com2/aab completed" } "/aac" { HTTP::respond 302 Location "https://www.test.com/aac" #log local0. "redirect to https://www.test.com2/aac completed" } "/aad" { HTTP::respond 302 Location "https://www.test.com/aad" #log local0. "redirect to https://www.test.com2/aad completed" } "/aae" { HTTP::respond 302 Location "https://www.test.com/aae" #log local0. "redirect to https://www.test.com2/aae completed" } "/aaf" { HTTP::respond 302 Location "https://www.test.com/aaf" #log local0. "redirect to https://www.test.com2/aaf completed" } "/aag" { HTTP::respond 302 Location "https://www.test.com/aag" #log local0. "redirect to https://www.test.com2/aag completed" } default { # default action to perform if URI doesn't match return } } } }
Thanks
- PauliusJun 05, 2023MVP
veredgfbll You can do that but you don't have to because the default action without defining a section called "default" would be to proceed as normal. This applies to the else in an if else statement, if you didn't define an else action the iRule would continue on as normal.
- veredgfbllJun 05, 2023Cirrus
Just to clarify - I can leave the default empty?
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