Forum Discussion
I'm trying to write an irule with multiple not scripts in it
I'm trying to write an irule with multiple not scripts in it
13 Replies
- Kevin_Stewart
Employee
not scripts?
Can you elaborate?
- f5_rocks
Nimbostratus
I'm working on a project which requires migrating most of the urls for an application suite to similar, but different hostnames. I started with this irule for one url string, but now, they want to keep some of the urls the way they were and some not, so the irule comparison needs to start at a higher level, but not redirect some specific substrings.
I started with this:
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/web/abcd.client.web/"} { if {$MY_HOST equals "m3.companyname.com"} { HTTP::redirect http://a1.m3.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3.companyname.com"} { HTTP::redirect http://internal.a1.m3.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc1.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc1.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc2.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc2.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc3.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc3.companyname.com$MY_URI } event HTTP_REQUEST disable TCP::close } }
when HTTP_RESPONSE {event HTTP_REQUEST enable}tried this, get syntax/format errors
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/") } elseif {not ($MY_URI starts_with "/abcd/web/abcdpaymentprocessing.web/")} { if {$MY_HOST equals "m3.companyname.com"} { HTTP::redirect http://a1.m3.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3.companyname.com"} { HTTP::redirect http://internal.a1.m3.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc1.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc1.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc2.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc2.companyname.com$MY_URI } elseif {$MY_HOST equals "internal.m3-dc3.companyname.com"} { HTTP::redirect http://internal.a1.m3-dc3.companyname.com$MY_URI } event HTTP_REQUEST disable TCP::close } } }
when HTTP_RESPONSE {event HTTP_REQUEST enable}could use "or", f5 accepted it as syntactically correct, but this try didn't work, still redirected the "not" uris...
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/") or not ($MY_URI starts_with "/abcd/web/abcdpaymentprocessing.web/") } { if {$MY_HOST equals "m3.companyname"} { HTTP::redirect $MY_URI } elseif {$MY_HOST equals "internal.m3.companyname"} { HTTP::redirect $MY_URI } elseif {$MY_HOST equals "internal.m3-dc1.companyname"} { HTTP::redirect $MY_URI } elseif {$MY_HOST equals "internal.m3-dc2.companyname"} { HTTP::redirect $MY_URI } elseif {$MY_HOST equals "internal.m3-dc3.companyname"} { HTTP::redirect $MY_URI } event HTTP_REQUEST disable TCP::close } } } when HTTP_RESPONSE {event HTTP_REQUEST enable}
I thought of using a generic "contains", of "/abcd/web/abcdpayment" but I expect more very specific surgical type requests may be desired in the future.
I think using "or" or some other type of comparison must be the better choice, but I may not be skilled enough to find the answer in the time window required.
I appreciate any help you could offer. (I don't know why the last line in the irule is blown up so large)
Hello,
The code posted below is not well understandable. But I think that you should prefer the switch command instead of if elseif else.
Here is a link that explain how to use switch : https://devcentral.f5.com/questions/how-to-use-an-irule-switch-to-match-httphost-and-httpuri
- f5_rocks
Nimbostratus
Sorry about the formatting. It's my first question and I'm trying to figure out how to keep the original formatting. I'll look at switch....Well, I figured out how to format properly, but the forum's algorithims think it's spam, so... - for the formatting, you can post a new answer, select the whole code and click on the Preformatted Code button in the pseudo wysiwyg toolbar
- f5_rocks
Nimbostratus
Yes. Thank you. I reposted as another question using proper formatting and got an answer on how to write the script using "if {not (a or b)}" or "if {not (a) and not (b)}. I was misunderstanding the correct usage and result of "or".
- Yann_Desmarest_
Nacreous
Hello,
The code posted below is not well understandable. But I think that you should prefer the switch command instead of if elseif else.
Here is a link that explain how to use switch : https://devcentral.f5.com/questions/how-to-use-an-irule-switch-to-match-httphost-and-httpuri
- f5_rocks
Nimbostratus
Sorry about the formatting. It's my first question and I'm trying to figure out how to keep the original formatting. I'll look at switch....Well, I figured out how to format properly, but the forum's algorithims think it's spam, so... - Yann_Desmarest_
Nacreous
for the formatting, you can post a new answer, select the whole code and click on the Preformatted Code button in the pseudo wysiwyg toolbar - f5_rocks
Nimbostratus
Yes. Thank you. I reposted as another question using proper formatting and got an answer on how to write the script using "if {not (a or b)}" or "if {not (a) and not (b)}. I was misunderstanding the correct usage and result of "or".
- Stanislas_Piro2
Cumulonimbus
Hi,
you can try to use array to define old and new hostnames:
when RULE_INIT { array set static::NewURL { m3.companyname a1.m3.companyname internal.m3.companyname internal.a1.m3.companyname internal.m3-dc1.companyname internal.a1.m3-dc1.companyname internal.m3-dc2.companyname internal.a1.m3-dc2.companyname internal.m3-dc3.companyname internal.a1.m3-dc3.companyname } } when HTTP_REQUEST { switch -glob [string tolower [HTTP::path]] { "/abcd/web/abcdpayment.web/*" - "/abcd/web/abcdpaymentprocessing.web/*" {} "/abcd/*" { if {[info exists static::NewURL([HTTP::host])]} {HTTP::redirect http://$static::NewURL([HTTP::host])[string tolower [HTTP::uri]]} } } }
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