# irule help based on uri

**URL:** <https://community.f5.com/t/irule-help-based-on-uri/53850>\
**Category:** Technical Forums\
**Tags:** irules, security, ltm\
**Created:** [September 3, 2019, 1:49pm UTC](https://community.f5.com/t/irule-help-based-on-uri/53850 "2019-09-03T13:49:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymru81](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@cymru81](https://community.f5.com/u/cymru81)\
**Post date:** [September 3, 2019, 1:49pm UTC](https://community.f5.com/t/irule-help-based-on-uri/53850/1 "2019-09-03T13:49:09Z")

</div>

Hi, ive done some searching on here but cannot find the exact help i need. We have the following scenario, can someone assist with an irule that will achieve this please?

we have one VIP that hostname [site.abc.com](http://site.abc.com) resolves to, if user visits [https://site.abc.com/live](https://site.abc.com/live) it should redirect to pool “pool\_live” maintaining the uri “/live”. If user visits [https://site.abc.com/test](https://site.abc.com/test) it should redirect to pool “pool\_test” again maintaining the uri.

how can we achieve this?

thanks in advance!

---

<div class="post-metadata">

**Author:** ![iaine](https://avatars.discourse-cdn.com/v4/letter/i/f07891/32.png) [@iaine](https://community.f5.com/u/iaine)\
**Post date:** [September 3, 2019, 2:01pm UTC](https://community.f5.com/t/irule-help-based-on-uri/53850/2 "2019-09-03T14:01:48Z")

</div>

Hi

You could do something like

```tcl
when HTTP_REQUEST {
	if {[HTTP::uri] starts_with "/live"}{
		pool pool_live
	} elseif {[HTTP::uri] starts_with "/test"}{
		pool pool_test
	}
}

```

---

<div class="post-metadata">

**Author:** ![DanS92](https://avatars.discourse-cdn.com/v4/letter/d/c6cbf5/32.png) [@DanS92](https://community.f5.com/u/DanS92)\
**Post date:** [September 3, 2019, 2:36pm UTC](https://community.f5.com/t/irule-help-based-on-uri/53850/3 "2019-09-03T14:36:01Z")

</div>

Another way would be with a Local Traffic Policy instead of an iRule. Under Local Traffic \> Policies \> Policy List, create a new policy. You’d need a rule in the policy like this for the first request:

 ![image_240797.jpg](https://d20hrnpixdzcsd.cloudfront.net/original/2X/f/fbcfe82d97e3262663c1e8b283ab8e183f416f4a.jpeg)

The rule for the second URI would be built the same way. Then apply the policy to the VIP.

---

<div class="post-metadata">

**Author:** ![cymru81](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@cymru81](https://community.f5.com/u/cymru81)\
**Post date:** [September 4, 2019, 8:09am UTC](https://community.f5.com/t/irule-help-based-on-uri/53850/4 "2019-09-04T08:09:24Z")

</div>

Im sorry to move the goalposts but my brief has changed to the following now:

we have one VIP that hostname&nbsp;[site.abc.com](http://site.abc.com/)&nbsp;resolves to, if user visits&nbsp;[https://site.abc.com/live](https://site.abc.com/live)&nbsp;it should redirect to pool “pool\_live” changing the ur toi “/live\_site”. If user visits&nbsp;[https://site.abc.com/test](https://site.abc.com/test)&nbsp;it should redirect to pool “pool\_test” changing the uri to /test\_site.

Is this achievable?

---

<div class="post-metadata">

**Author:** ![Enes\_Afsin\_Al](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/enes_afsin_al/32/15359_2.png) [@Enes\_Afsin\_Al](https://community.f5.com/u/Enes_Afsin_Al)\
**Post date:** [September 4, 2019, 9:03am UTC](https://community.f5.com/t/irule-help-based-on-uri/53850/5 "2019-09-04T09:03:21Z")

</div>

Hi cymru81,

iRule:

```tcl
when HTTP_REQUEST {
	if { [string tolower [HTTP::host]] equals "site.abc.com" } {
		switch -glob [string tolower [HTTP::uri]] {
			"/live" {
				HTTP::uri "/live_site"
				pool pool_live
			}
			"/test" {
				HTTP::uri "/test_site"
				pool pool_test
			}
		}
	}
}

```

iRule with redirect:

```tcl
when HTTP_REQUEST {
	if { [string tolower [HTTP::host]] equals "site.abc.com" } {
		switch -glob [string tolower [HTTP::uri]] {
			"/live" {
				HTTP::redirect "https://site.abc.com/live_site"
				pool pool_live
			}
			"/test" {
				HTTP::redirect "https://site.abc.com/test_site"
				pool pool_test
			}
		}
	}
}

```

If the HTTP::host is not [site.abc.com](http://site.abc.com), you can reject requests.

---

<div class="post-metadata">

**Author:** ![cymru81](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@cymru81](https://community.f5.com/u/cymru81)\
**Post date:** [September 4, 2019, 9:13am UTC](https://community.f5.com/t/irule-help-based-on-uri/53850/6 "2019-09-04T09:13:38Z")

</div>

thank you the first rule is just the job!

ive got another issue now, it works as expected so if a user goes to [https://site.abc.com/live](https://site.abc.com/live) they end up at [https://site.abc.com/live\_site](https://site.abc.com/live%5C_site) but if they try to navigate any subsequent links it fails.

Basically we have an external url [https://site.abc.com/live\_site/](https://site.abc.com/live%5C_site/) that we want a proxy mapping that will go to [http://internalserver:1234/live](http://internalserver:1234/live) - we have a pool “pool\_live” that contains “internalserver:1234”.

is this totally achievable or am i missing something fundamental!

---

<div class="post-metadata">

**Author:** ![iaine](https://avatars.discourse-cdn.com/v4/letter/i/f07891/32.png) [@iaine](https://community.f5.com/u/iaine)\
**Post date:** [September 4, 2019, 10:34am UTC](https://community.f5.com/t/irule-help-based-on-uri/53850/7 "2019-09-04T10:34:03Z")

</div>

Hi

To keep the rest of the path in place, use something like this. This will replace /live with /live\_site and then remove the first 5 characters from the uri (so /live) and then insert the rest of the path

```tcl
when HTTP_REQUEST {
	if {[HTTP::uri] starts_with "/live"}{
		HTTP::uri /live_site[string range [HTTP::uri] 5 end]
		pool pool_live
	} elseif {[HTTP::uri] starts_with "/test"}{
		HTTP::uri /test_site[string range [HTTP::uri] 5 end]
		pool pool_test
	}
}

```

---

<div class="post-metadata">

**Author:** ![cymru81](https://avatars.discourse-cdn.com/v4/letter/c/e19b73/32.png) [@cymru81](https://community.f5.com/u/cymru81)\
**Post date:** [September 4, 2019, 2:01pm UTC](https://community.f5.com/t/irule-help-based-on-uri/53850/8 "2019-09-04T14:01:01Z")

</div>

thank you once again! ive managed to get this working now with a local traffic policy and a rewrite profile, think some luck involved too!
