# Url redirection

**URL:** <https://community.f5.com/t/url-redirection/72474>\
**Category:** Technical Forums\
**Tags:** devops, security, event, application-delivery\
**Created:** [June 7, 2023, 2:35pm UTC](https://community.f5.com/t/url-redirection/72474 "2023-06-07T14:35:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mj16othman](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mj16othman](https://community.f5.com/u/mj16othman)\
**Post date:** [June 7, 2023, 2:35pm UTC](https://community.f5.com/t/url-redirection/72474/1 "2023-06-07T14:35:38Z")

</div>

Hi guys I need your need.

Let’s say I have a url ([example.com/apps/test.](http://example.com/apps/test.))

I want this exact url to work, and anything else that’s not that url to redirect to ([example.com](http://example.com))

---

<div class="post-metadata">

**Author:** ![Mohamed\_Ahmed\_Kansoh](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/mohamed_ahmed_kansoh/32/10886_2.png) [@Mohamed\_Ahmed\_Kansoh](https://community.f5.com/u/Mohamed_Ahmed_Kansoh)\
**Post date:** [June 7, 2023, 2:55pm UTC](https://community.f5.com/t/url-redirection/72474/2 "2023-06-07T14:55:45Z")

</div>

Hi&nbsp; @mj16othman &nbsp;,&nbsp;  
Try this :

```tcl
when HTTP_REQUEST {
if { [string tolower "[HTTP::host][HTTP::path]"] contains "example.com/apps/test" }{
	return 
	} else { 
	
	#HTTP::redirect "http://example.com"
	HTTP::redirect "http://[HTTP::host]"
	}
}

```

\> you must assign the default pool to that virtual server.&nbsp;  
use only one of those redirections&nbsp; :&nbsp;  
HTTP::redirect “[http://example.com](http://example.com)”  
HTTP::redirect “http://[HTTP::host]”

I hope this helps you

---

<div class="post-metadata">

**Author:** ![whisperer](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/whisperer/32/13906_2.png) [@whisperer](https://community.f5.com/u/whisperer)\
**Post date:** [June 8, 2023, 3:10am UTC](https://community.f5.com/t/url-redirection/72474/3 "2023-06-08T03:10:46Z")

</div>

The iRule posted by&nbsp;&nbsp;should work, but you can clean it up a bit. Here we do the following:- More efficient string comparison using starts\_with rather than contains. Also, more secure as a URL may be odd and repeat that string as part of URI path (unlikely though).

- Removed unnecessary comparison for host header. Likely there is no virtual hosting, so all (and only) [example.com](http://example.com) traffic hitting the virtual server. We only need it for return if path doesn’t match our pattern.

- Support both https and http protocols. In case you perform or what to add SSL termination or what not.

- You could also get fancier and support multiple paths you want to check via data groups, or even use a variable for the comparison string portion

So here we have it.

when HTTP\_REQUEST {  
if { [string tolower [HTTP::path]] starts\_with “/apps/test” }{  
return  
} else {  
if { [TCP::local\_port] == 443 }{  
HTTP::redirect “https://[HTTP::host]”  
} else {  
HTTP::redirect “http://[HTTP::host]”  
}  
}  
}

@Mohamed_Ahmed_Kansoh

---

<div class="post-metadata">

**Author:** ![whisperer](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/whisperer/32/13906_2.png) [@whisperer](https://community.f5.com/u/whisperer)\
**Post date:** [June 8, 2023, 3:13am UTC](https://community.f5.com/t/url-redirection/72474/4 "2023-06-08T03:13:29Z")

</div>

Sorry for the formatting, but posting TCL code via the iPhone triggers an error on DevCentral!

"Correct the highlighted errors and try again.

Post flooding detected (community received posts of a unique message more than 1 times within 3,600 seconds)"

Thats even with just a simple “test” using the TCL \</\> option in the WYSIWYG editor. Strange.

---

<div class="post-metadata">

**Author:** ![Mohamed\_Ahmed\_Kansoh](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/mohamed_ahmed_kansoh/32/10886_2.png) [@Mohamed\_Ahmed\_Kansoh](https://community.f5.com/u/Mohamed_Ahmed_Kansoh)\
**Post date:** [June 8, 2023, 8:50am UTC](https://community.f5.com/t/url-redirection/72474/5 "2023-06-08T08:50:30Z")

</div>

Hi&nbsp; @whisperer &nbsp;,&nbsp;  
Thanks a lot ,&nbsp;  
I have tested it in my internal Lab , it worked as expected for HTTP only however he can do the same for HTTPS but with offloading and redirecting to https at least.&nbsp;  
@mj16othman

---

<div class="post-metadata">

**Author:** ![whisperer](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/whisperer/32/13906_2.png) [@whisperer](https://community.f5.com/u/whisperer)\
**Post date:** [June 8, 2023, 11:29am UTC](https://community.f5.com/t/url-redirection/72474/6 "2023-06-08T11:29:40Z")

</div>

@Mohamed_Ahmed_Kansoh &nbsp;Correct, for HTTPS you would need to have an appropriate VS with client SSL profile and listening on standard port 443. I always think about writing generic iRules so you can resume them for multiple applications.

---

<div class="post-metadata">

**Author:** ![Mohamed\_Ahmed\_Kansoh](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/mohamed_ahmed_kansoh/32/10886_2.png) [@Mohamed\_Ahmed\_Kansoh](https://community.f5.com/u/Mohamed_Ahmed_Kansoh)\
**Post date:** [June 8, 2023, 12:48pm UTC](https://community.f5.com/t/url-redirection/72474/7 "2023-06-08T12:48:48Z")

</div>

@whisperer &nbsp;,&nbsp;  
I got you , well I like this way to normalize everything to be generic&nbsp;😉

---

<div class="post-metadata">

**Author:** ![Leslie\_Hubertus](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/leslie_hubertus/32/11644_2.png) [@Leslie\_Hubertus](https://community.f5.com/u/Leslie_Hubertus)\
**Post date:** [June 13, 2023, 12:29am UTC](https://community.f5.com/t/url-redirection/72474/8 "2023-06-13T00:29:07Z")

</div>

Tagging&nbsp; @LiefZimmerman &nbsp;so he can see your issue with sharing TCL code from your phone triggering a post flooding timeout.
