# Redirection iRule

**URL:** <https://community.f5.com/t/redirection-irule/23148>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [May 11, 2009, 7:01pm UTC](https://community.f5.com/t/redirection-irule/23148 "2009-05-11T19:01:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Puneet\_73909](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@Puneet\_73909](https://community.f5.com/u/Puneet_73909)\
**Post date:** [May 11, 2009, 7:01pm UTC](https://community.f5.com/t/redirection-irule/23148/1 "2009-05-11T19:01:31Z")

</div>

Hi,

I need help in writing an iRule. I want to read domain name and then redirect it to specific URL.

For example if HHTP::request is for [www.domain.com](http://www.domain.com) then it will redirect it to [www.abc.com](http://www.abc.com) and if request is for [www1.domain.com](http://www1.domain.com) then it will redirect it to [www.xxyz.com](http://www.xxyz.com).

In addition to this if request is for [http://www.DomainName.com/doc](http://www.DomainName.com/doc) redirects to [http://www.DomainName.com/ecom/pages/nm/nmhomepage.jsp](http://www.DomainName.com/ecom/pages/nm/nmhomepage.jsp) and same goes for

[http://www1.DomainName.com/minisite](http://www1.DomainName.com/minisite) redirects to [http://www1.DomainName.com/ecom/pages/nm/nmhomepage.jsp](http://www1.DomainName.com/ecom/pages/nm/nmhomepage.jsp)

All requests to [http://www.joinDomainName.com](http://www.joinDomainName.com) gets forwarded to [http://www.joinDomainName.com](http://www.joinDomainName.com) and [http://www1.joinDomainName.com](http://www1.joinDomainName.com) gets forwarded to [http://www1.joinDomainName.com](http://www1.joinDomainName.com)

Please help!!

Regards,

Puneet Khanna

---

<div class="post-metadata">

**Author:** ![The\_Bhattman](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/the_bhattman/32/13958_2.png) [@The\_Bhattman](https://community.f5.com/u/The_Bhattman)\
**Post date:** [May 11, 2009, 9:42pm UTC](https://community.f5.com/t/redirection-irule/23148/2 "2009-05-11T21:42:29Z")

</div>

Hi Puneet,

I believe you can write your logic in the following manner

```tcl
when HTTP_REQUEST { 
 set h [HTTP::host] 
 set u [HTTP::uri] 
 switch -glob $h { 
 "www.domain.com" { 
 HTTP::redirect "http://www.abc.com" 
 } 
 "www1.domain.com" { 
 HTTP::redirect "http://www.xxyz.com" 
 } 
 "www.domainname.com" - 
 "www1.domainname.com" { 
 switch -glob $u { 
 "/doc" - 
 "/minisite" { 
 HTTP::redirect "http://[HTTP::host]/econ/pages/nm/nmhomepage.jsp" 
 } 
 } 
 } 
 } 
  
 }

```

[Click here](http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=129) to look at the reference to using switch command

However, I am not exactly sure what you mean about your comment

“All requests to [http://www.joinDomainName.com](http://www.joinDomainName.com) gets forwarded to [http://www.joinDomainName.com](http://www.joinDomainName.com) and [http://www1.joinDomainName.com](http://www1.joinDomainName.com) gets forwarded to [http://www1.joinDomainName.com](http://www1.joinDomainName.com)”

Hope this helps

CB

---

<div class="post-metadata">

**Author:** ![Puneet\_73909](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@Puneet\_73909](https://community.f5.com/u/Puneet_73909)\
**Post date:** [May 11, 2009, 10:05pm UTC](https://community.f5.com/t/redirection-irule/23148/3 "2009-05-11T22:05:43Z")

</div>

Thnx CB..

What I mean to say is the way I am matching “www” and redirecting it to some url and “www1” to some url. I want the redirection if I get [www.joinxyz.com](http://www.joinxyz.com) (join keyword in domain name) to [www.joinxyz.com/doc/index.html](http://www.joinxyz.com/doc/index.html) and same goes for [www1.joinxyz.com](http://www1.joinxyz.com) to [www1.joinxyz.com/doc/index.html](http://www1.joinxyz.com/doc/index.html).

Given below is the iRULE which wrote but I have to add the JOIN keyword rule in this…

when HTTP\_REQUEST {

Check if requested host start with [www.example.com](http://www.example.com)

if {[string tolower [HTTP::host]] equals “[www.example.com](http://www.example.com)”} {

HTTP::redirect “[http://www.DomainName.com/nmhomepage.jsp](http://www.DomainName.com/nmhomepage.jsp)”}

elseif {[string tolower [HTTP::host]] starts\_with “www1.”} {

Replace the host header value with [www1.example.com](http://www1.example.com)

HTTP::redirect “[http://www1.DomainName.com/nmhomepage.jsp](http://www1.DomainName.com/nmhomepage.jsp)”}

elseif {([HTTP::uri] starts\_with “doc” or “minisite” or “fslink” or “ms” or “link” or “null-request” or “exttracking.dyn”)} {

Redirection

HTTP::redirect “http://[HTTP::host]/nmhomepage.jsp”}

}

Will this work???

---

<div class="post-metadata">

**Author:** ![The\_Bhattman](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/the_bhattman/32/13958_2.png) [@The\_Bhattman](https://community.f5.com/u/The_Bhattman)\
**Post date:** [May 11, 2009, 11:00pm UTC](https://community.f5.com/t/redirection-irule/23148/4 "2009-05-11T23:00:36Z")

</div>

I think it will work. But I am not sure if this is the most efficient way to have request run through

Here is how I look at this code. Let us know if that is what you were intending:

The first conditional statement will check the host name [www.example.com](http://www.example.com) and if it matches then redirect and exit out of the event. If it does not then go to the second elseif statement. If the domain starts with www1 then redirect and exit out of the event. This is equivalent to www1\* wildcard so pretty much any domain starting with www1 will match this statement. If that doesn’t match then it will go to the 3rd elseif. At this point it will not care which domain(except for whatever matches in the previous conditional statement) as so long as it matches starts with whatever is in the URI for example it can be /document or /minisites or /msconfig. As you can see it just needs to match the STARTING letters. This is something you might want to consider because it promotes bad entries. Otherwise if it matches it does a redirect and then exists out of the event.

I hope this helps you out.

CB

---

<div class="post-metadata">

**Author:** ![Puneet\_73909](https://avatars.discourse-cdn.com/v4/letter/p/ac8455/32.png) [@Puneet\_73909](https://community.f5.com/u/Puneet_73909)\
**Post date:** [May 12, 2009, 2:41am UTC](https://community.f5.com/t/redirection-irule/23148/5 "2009-05-12T02:41:52Z")

</div>

CM you are right!!!

This is what I intented top do..I hope this rule will work and also I need to add LB\_failed in this so that if my webserver goes unreachable then it will get redirected to maintainance paige. I am unable to do so.

So I need to add another rule saying…

when LB\_FAILED {

if {[active\_members [LB::server pool]] \> 0 } {

Active Pool

pool “Vs\_Server”}

else {HTTP::redirect “[http://www1.DomainName.com/ecom/pages/nm/nmhomepage.jsp](http://www1.DomainName.com/ecom/pages/nm/nmhomepage.jsp)”}

Fallback path

}

I just want to add this is my previous rule. Is it possible or I have to create another seperate rule and refer it in same Virtual Server. Will it be a best practice to do refer two seperate iRules in same Virtual Server??
