# Irule logic question

**URL:** <https://community.f5.com/t/irule-logic-question/20857>\
**Category:** Technical Forums\
**Tags:** irules, application-delivery, ltm\
**Created:** [January 10, 2018, 1:48pm UTC](https://community.f5.com/t/irule-logic-question/20857 "2018-01-10T13:48:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee\_Wooderson\_1](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lee\_Wooderson\_1](https://community.f5.com/u/Lee_Wooderson_1)\
**Post date:** [January 10, 2018, 1:48pm UTC](https://community.f5.com/t/irule-logic-question/20857/1 "2018-01-10T13:48:41Z")

</div>

when HTTP\_REQUEST { if { ([matchclass [string tolower [HTTP::uri]] contains Allowed\_uri]) or ([matchclass [IP::client\_addr] equals Allowed\_IP]) } { } else {

log local0. “---CLIENT IP---[IP::client\_addr] URI is [HTTP::uri]”

drop }

In the above irule, would the http request be permitted as long as either datagroup matches? It wouldn’t have to match both to be permitted because of the “or” logic?

---

<div class="post-metadata">

**Author:** ![Lee\_Sutcliffe](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@Lee\_Sutcliffe](https://community.f5.com/u/Lee_Sutcliffe)\
**Post date:** [January 10, 2018, 2:03pm UTC](https://community.f5.com/t/irule-logic-question/20857/2 "2018-01-10T14:03:25Z")

</div>

Hi Lee,

I sometimes find it easier to read an iRule (especially when you include NOTs) by breaking the ‘or’ into separate ‘if’ conditions. Notice the “!” - this makes the condition a NOT.

So:

IF NOT datagroup URI, IF NOT datagoup IP, drop. (everything else will be allowed)

```tcl
when HTTP_REQUEST {
    if {(![class match [string tolower [HTTP::uri]] contains Allowed_uri])} {
        if {(![class match [IP::client_addr] equals Allowed_IP])} {
            drop
        }
    }
}

```

PS -

```tcl
matchclass

```

has been depricated: [DevCentral](https://devcentral.f5.com/wiki/iRules.matchclass.ashx)

Lee

---

<div class="post-metadata">

**Author:** ![Andy\_McGrath](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/andy_mcgrath/32/28642_2.png) [@Andy\_McGrath](https://community.f5.com/u/Andy_McGrath)\
**Post date:** [January 10, 2018, 2:32pm UTC](https://community.f5.com/t/irule-logic-question/20857/3 "2018-01-10T14:32:58Z")

</div>

Your iRule is almost correct but logic needs to be change from OR to AND:

```tcl
when HTTP_REQUEST {
    if {[matchclass [string tolower [HTTP::uri]] contains Allowed_uri] and [matchclass [IP::client_addr] equals Allowed_IP]} {
        return
     } else {
        log local0. "---CLIENT IP---[IP::client_addr] URI is [HTTP::uri]"
        drop
    }
}

```

reversing the logic, like MrPlastic has done, you can do the same with less using _not_ and logic OR:

```tcl
when HTTP_REQUEST {
    if {(not [matchclass [string tolower [HTTP::uri]] contains Allowed_uri]) or (not [matchclass [IP::client_addr] equals Allowed_IP])} {
        log local0. "---CLIENT IP---[IP::client_addr] URI is [HTTP::uri]"
        drop
    }
}

```

---

<div class="post-metadata">

**Author:** ![Lee\_Wooderson\_1](https://avatars.discourse-cdn.com/v4/letter/l/45deac/32.png) [@Lee\_Wooderson\_1](https://community.f5.com/u/Lee_Wooderson_1)\
**Post date:** [January 10, 2018, 3:51pm UTC](https://community.f5.com/t/irule-logic-question/20857/4 "2018-01-10T15:51:49Z")

</div>

How would I rewrite my irule if I only wanted to permit URIs in Allowed\_uri and just use the one datagroup and forget the source IP filter? Thanks for ur guys help.

---

<div class="post-metadata">

**Author:** ![Lee\_Sutcliffe](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@Lee\_Sutcliffe](https://community.f5.com/u/Lee_Sutcliffe)\
**Post date:** [January 10, 2018, 4:31pm UTC](https://community.f5.com/t/irule-logic-question/20857/5 "2018-01-10T16:31:10Z")

</div>

This should be all you need, traffic not matching the URI data group will be dropped. Everything else will be permitted.

```tcl
when HTTP_REQUEST {
    if {(not [class match [string tolower [HTTP::uri]] contains Allowed_uri])} {
        drop
    }
}

```
