Forum Discussion
f5_rocks
Nimbostratus
Jun 01, 2016How to expand irule using switch or logical or in selection process?
Developers want to add more "lanes" for multiple branches, so I want to add 3 more versions of "starts_with" and make the "if nots" more generic so they can apply to all the "starts_with" versions. I've never tried using switch or a logical or in the selection process before. Do either or both attempts look correct?
====================================================
abcd_redirect-http (original)
------------------------------------------
when HTTP_RESPONSE {event HTTP_REQUEST enable}
when CLIENT_ACCEPTED {event HTTP_REQUEST enable}
when HTTP_REQUEST {
set MY_HOST [string tolower [HTTP::host]]
set MY_URI [string tolower [HTTP::uri]]
if {$MY_URI starts_with "/abcd/"} {
if {not ($MY_URI starts_with "/abcd/web/abcdpayment.web/") and not ($MY_URI starts_with "/abcd/web/abcdpaymentprocessing.web/")} {
if {$MY_HOST equals "m3.corpname.com"} {
HTTP::redirect http://a1.m3.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3.corpname.com"} {
HTTP::redirect http://intra.a1.m3.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc1.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc1.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc2.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc2.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc3.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc3.corpname.com$MY_URI
}
event HTTP_REQUEST disable
TCP::close
}
}
}
when HTTP_RESPONSE {event HTTP_REQUEST enable}
====================================================
abcd_redirect-http (trying switch -glob)
------------------------------------------
when HTTP_RESPONSE {event HTTP_REQUEST enable}
when CLIENT_ACCEPTED {event HTTP_REQUEST enable}
when HTTP_REQUEST {
set MY_HOST [string tolower [HTTP::host]]
set MY_URI [string tolower [HTTP::uri]]
switch -glob $MY_URI {
"/abcd/"-
"/abcd_lane2"-
"/abcd_hotfix" {
if {not ($MY_URI contains "/web/abcdpayment.web/") and not ($MY_URI contains "/web/abcdpaymentprocessing.web/")} {
if {$MY_HOST equals "m3.corpname.com"} {
HTTP::redirect http://a1.m3.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3.corpname.com"} {
HTTP::redirect http://intra.a1.m3.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc1.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc1.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc2.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc2.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc3.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc3.corpname.com$MY_URI
}
event HTTP_REQUEST disable
TCP::close
}
}
}
when HTTP_RESPONSE {event HTTP_REQUEST enable}
===============================================================
abcd_redirect-http (trying logical or ||)
------------------------------------------
when HTTP_RESPONSE {event HTTP_REQUEST enable}
when CLIENT_ACCEPTED {event HTTP_REQUEST enable}
when HTTP_REQUEST {
set MY_HOST [string tolower [HTTP::host]]
set MY_URI [string tolower [HTTP::uri]]
if {$MY_URI starts_with "/abcd/" || $MY_URI starts_with "/abcd_lane2/" || $MY_URI starts_with "/abcd_hotfix/" } {
if {not ($MY_URI contains "/web/abcdpayment.web/") and not ($MY_URI contains "/web/abcdpaymentprocessing.web/")} {
if {$MY_HOST equals "m3.corpname.com"} {
HTTP::redirect http://a1.m3.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3.corpname.com"} {
HTTP::redirect http://intra.a1.m3.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc1.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc1.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc2.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc2.corpname.com$MY_URI
} elseif {$MY_HOST equals "intra.m3-dc3.corpname.com"} {
HTTP::redirect http://intra.a1.m3-dc3.corpname.com$MY_URI
}
event HTTP_REQUEST disable
TCP::close
}
}
}
when HTTP_RESPONSE {event HTTP_REQUEST enable}
===============================================================
3 Replies
- Vijay_E
Cirrus
-
Usually, when you add the iRule to the F5 LTM, it will throw one or more errors if there are syntax issues. Have you tried adding it to the LTM & do you see any errors ?
-
See this for switch: https://devcentral.f5.com/articles/irules-101-04-switch
-
I find the explicit event enabling commands "HTTP_REQUEST enable" redundant. I may be mistaken but I am not sure why you have to enable the event.
-
- Stanislas_Piro2
Cumulonimbus
Hi,
you can try this irule:
when HTTP_REQUEST { set MY_HOST [string tolower [HTTP::host]] set MY_URI [string tolower [HTTP::uri]] switch -glob $MY_URI { "*/web/abcdpayment.web/*" - "*/web/abcdpaymentprocessing.web/*" {TCP::close} "/abcd/*" - "/abcd_lane2/*" - "/abcd_hotfix/*" { switch $MY_HOST { "m3.corpname.com" {HTTP::redirect http://a1.m3.corpname.com$MY_URI} "intra.m3.corpname.com" {HTTP::redirect http://intra.a1.m3.corpname.com$MY_URI} "intra.m3-dc1.corpname.com" {HTTP::redirect http://intra.a1.m3-dc1.corpname.com$MY_URI} "intra.m3-dc2.corpname.com" {HTTP::redirect http://intra.a1.m3-dc2.corpname.com$MY_URI} "intra.m3-dc3.corpname.com" {HTTP::redirect http://intra.a1.m3-dc3.corpname.com$MY_URI} } TCP::close } } } - f5_rocks
Nimbostratus
The proposed solution did not work, so I started over with my rule and incorporated the easy switch for the hostnames. when CLIENT_ACCEPTED {event HTTP_REQUEST enable} when HTTP_REQUEST { set MY_HOST [string tolower [HTTP::host]] set MY_URI [string tolower [HTTP::uri]] if {$MY_URI starts_with "/abcd"} { if {not ($MY_URI contains "/abcdpayment.web/") and not ($MY_URI contains "/abcdpaymentprocessing.web/")} { switch $MY_HOST { "m3.corpname.com" {HTTP::redirect http://a1.m3.corpname.com$MY_URI} "intra.m3.corpname.com" {HTTP::redirect http://intra.a1.m3.corpname.com$MY_URI} "intra.m3-dc1.corpname.com" {HTTP::redirect http://intra.a1.m3-dc1.corpname.com$MY_URI} "intra.m3-dc2msp.corpname.com" {HTTP::redirect http://intra.a1.m3-dc2.corpname.com$MY_URI} "intra.m3-dc3.corpname.com" {HTTP::redirect http://intra.a1.m3-dc3.corpname.com$MY_URI} } } event HTTP_REQUEST disable TCP::close } } when HTTP_RESPONSE {event HTTP_REQUEST enable}
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects