# Need help with iRule based on host and path

**URL:** <https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782>\
**Category:** Technical Forums\
**Tags:** application-delivery\
**Created:** [July 10, 2024, 1:08pm UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782 "2024-07-10T13:08:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![adminas](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@adminas](https://community.f5.com/u/adminas)\
**Post date:** [July 10, 2024, 1:08pm UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/1 "2024-07-10T13:08:35Z")

</div>

Hello all.

I am trying to create an iRule that will choose a specific pool based on host and path. I tried the following code:

when HTTP\_REQUEST {  
&nbsp; if { [HTTP::host] equals “hostname\_a”} {  
&nbsp; &nbsp; if { [HTTP::path] equals “/path” } {

pool pool\_a  
&nbsp; &nbsp; &nbsp; &nbsp; log local0. “URL : [HTTP::host][HTTP::uri] entered”  
&nbsp; &nbsp; }  
&nbsp; }  
&nbsp; if { [HTTP::host] equals “hostname\_b”} {  
&nbsp; &nbsp; if { [HTTP::path] equals “/path” } {

pool pool\_b  
&nbsp; &nbsp; &nbsp; &nbsp; log local0. “URL : [HTTP::host][HTTP::uri] entered”  
&nbsp; &nbsp; }  
&nbsp; }&nbsp;  
}

According to Big-IP stats, the iRule gets executed but it doesnot direct the call to the pool (pool stats shows 0) specified although logging is invoked and I get the URL in the ltm logs.

Right after this script there is another one which gets executed and works as it should be. It specifies even more conditions for a series of pools and if none is matched then directs the call to the default pool specified there.

The problem is with the first one. I would like it to be executed and unless there is a match, then it should proceed to executing the second one.

If you can give me some advice it would be highly appreciated. Thank you.

---

<div class="post-metadata">

**Author:** ![JoseLabra](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/joselabra/32/6084_2.png) [@JoseLabra](https://community.f5.com/u/JoseLabra)\
**Post date:** [July 10, 2024, 1:21pm UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/2 "2024-07-10T13:21:47Z")

</div>

Hii how r u?

Why you dont use a Policy? Its more simple

check this article

> **[myF5](https://my.f5.com/manage/s/article/K15097)**

---

<div class="post-metadata">

**Author:** ![adminas](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@adminas](https://community.f5.com/u/adminas)\
**Post date:** [July 10, 2024, 1:36pm UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/3 "2024-07-10T13:36:09Z")

</div>

Hi. I am fine and hope the same for you!

Thank you very much for your reply but the are already 3 working iRules that I cannot remove / convert and I need just an extra iRule to be executed in the beggining and move to the next script if no match is found. Its a production system and I cannot really experiment as I have no experience in policies.

---

<div class="post-metadata">

**Author:** ![JoseLabra](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/joselabra/32/6084_2.png) [@JoseLabra](https://community.f5.com/u/JoseLabra)\
**Post date:** [July 10, 2024, 3:29pm UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/4 "2024-07-10T15:29:37Z")

</div>

And if you captured the traffic with tcpdump and test the iRule, you see the LB correctly?  
I remember a scenario with the same issue, the pool dont show the stats, but the LB its working.

if was the case, open a ticket to support for more detail

---

<div class="post-metadata">

**Author:** ![zamroni777](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/zamroni777/32/1473_2.png) [@zamroni777](https://community.f5.com/u/zamroni777)\
**Post date:** [July 10, 2024, 4:31pm UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/5 "2024-07-10T16:31:07Z")

</div>

“equals” means exact match.  
maybe what you need is starts\_with.

> **[starts\_with](https://clouddocs.f5.com/api/irules/starts_with.html)**

for testing, you can install virtualedition with trial license or create test vserver

---

<div class="post-metadata">

**Author:** ![Aswin\_mk](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/aswin_mk/32/5701_2.png) [@Aswin\_mk](https://community.f5.com/u/Aswin_mk)\
**Post date:** [July 10, 2024, 6:45pm UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/6 "2024-07-10T18:45:45Z")

</div>

Can u try with below example, u can add specific uri and host name. u can edit it based on your different host and path

when HTTP\_REQUEST {  
&nbsp; &nbsp; # Define pool selection based on host and path  
&nbsp; &nbsp; if { [HTTP::host] eq “[example.com](http://example.com)” } {  
&nbsp; &nbsp; &nbsp; &nbsp; if { [HTTP::uri] starts\_with “/app1” } {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pool pool\_app1  
&nbsp; &nbsp; &nbsp; &nbsp; } elseif { [HTTP::uri] starts\_with “/app2” } {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pool pool\_app2  
&nbsp; &nbsp; &nbsp; &nbsp; } else {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pool default\_pool  
&nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; } elseif { [HTTP::host] eq “[anotherexample.com](http://anotherexample.com)” } {  
&nbsp; &nbsp; &nbsp; &nbsp; if { [HTTP::uri] starts\_with “/service1” } {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pool pool\_service1  
&nbsp; &nbsp; &nbsp; &nbsp; } elseif { [HTTP::uri] starts\_with “/service2” } {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pool pool\_service2  
&nbsp; &nbsp; &nbsp; &nbsp; } else {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pool default\_pool  
&nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; } else {  
&nbsp; &nbsp; &nbsp; &nbsp; # Fallback to a default pool if no match is found  
&nbsp; &nbsp; &nbsp; &nbsp; pool default\_pool  
&nbsp; &nbsp; }  
}

---

<div class="post-metadata">

**Author:** ![adminas](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@adminas](https://community.f5.com/u/adminas)\
**Post date:** [July 11, 2024, 7:24am UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/7 "2024-07-11T07:24:42Z")

</div>

Thank you for your suggestion but there is no change.

---

<div class="post-metadata">

**Author:** ![adminas](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@adminas](https://community.f5.com/u/adminas)\
**Post date:** [July 11, 2024, 7:27am UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/8 "2024-07-11T07:27:22Z")

</div>

Thank you.. I tried with tcpdump -nnvvi but cannot see any request going to the pool specified. I can see only health checking.

---

<div class="post-metadata">

**Author:** ![adminas](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@adminas](https://community.f5.com/u/adminas)\
**Post date:** [July 11, 2024, 7:28am UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/9 "2024-07-11T07:28:12Z")

</div>

Thank you. Tried it, same result.

---

<div class="post-metadata">

**Author:** ![adminas](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@adminas](https://community.f5.com/u/adminas)\
**Post date:** [July 11, 2024, 10:32am UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/10 "2024-07-11T10:32:25Z")

</div>

Finally, problem solved! The reason was the order of execution for the 2 scripts. I donot really understand why this happens but when I placed the script in second place, everything is executed as expected. If you have any ideas please let me know.

The problematic order which doesnot work is shown below:

First script:

when HTTP\_REQUEST {  
&nbsp; if { [HTTP::host] equals “hostname\_a”} {  
&nbsp; &nbsp; if { [HTTP::path] equals “/path” } {

pool pool\_a  
&nbsp; &nbsp; &nbsp; &nbsp; log local0. “URL : [HTTP::host][HTTP::uri] entered”  
&nbsp; &nbsp; }  
&nbsp; }  
&nbsp; if { [HTTP::host] equals “hostname\_b”} {  
&nbsp; &nbsp; if { [HTTP::path] equals “/path” } {

pool pool\_b  
&nbsp; &nbsp; &nbsp; &nbsp; log local0. “URL : [HTTP::host][HTTP::uri] entered”  
&nbsp; &nbsp; }  
&nbsp; }&nbsp;  
}

Second script:

when HTTP\_REQUEST {  
if {[HTTP::has\_responded]}{return}  
&nbsp; &nbsp; switch -glob [string tolower [HTTP::path]]  
&nbsp; &nbsp; {

…

…

}

default  
&nbsp; &nbsp; {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch [string tolower [HTTP::host]]  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {

“hostname\_a”

{

pool pool\_c  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; persist none

}

…

…

default

{

pool\_z

}

…

Anyway, thank you all very much for your time.

---

<div class="post-metadata">

**Author:** ![Aswin\_mk](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/aswin_mk/32/5701_2.png) [@Aswin\_mk](https://community.f5.com/u/Aswin_mk)\
**Post date:** [July 11, 2024, 10:39am UTC](https://community.f5.com/t/need-help-with-irule-based-on-host-and-path/74782/11 "2024-07-11T10:39:10Z")

</div>

Good adminas.thanks for the confirmation 🙂
