# iRule Rewrite Question

**URL:** <https://community.f5.com/t/irule-rewrite-question/2170>\
**Category:** Technical Forums\
**Tags:** big-ip, irules, application-delivery\
**Created:** [April 21, 2016, 5:58pm UTC](https://community.f5.com/t/irule-rewrite-question/2170 "2016-04-21T17:58:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slumlorde\_21011](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@Slumlorde\_21011](https://community.f5.com/u/Slumlorde_21011)\
**Post date:** [April 21, 2016, 5:58pm UTC](https://community.f5.com/t/irule-rewrite-question/2170/1 "2016-04-21T17:58:01Z")

</div>

I’ve got a iRule that I would like to edit so that when I direct someone to [marvel.somedomain.net](http://marvel.somedomain.net) pool it redirects to either [kibana.somedomain.net](http://kibana.somedomain.net) or [marvel.somedomain.net](http://marvel.somedomain.net) and appends the url with /app/kibana or /app/marvel depending on what site they are trying to reach. How would I go about this?

Both of these apps run off of the same port on the servers in the pool which is why I think I need a solution for this. If there is a better way to go about it I’m open to anything.

```tcl
from: https://devcentral.f5.com/articles/name-based-virtual-hosting-with-ltm.Unfi8eLjWW5
    when HTTP_REQUEST {
            switch -glob [string tolower [HTTP::host]] {

                    marvel.somedomain.net { pool EXT_marvel.somedomain.net_https_pool }

            default { reject }
        }

     }

```

---

<div class="post-metadata">

**Author:** ![VFB](https://avatars.discourse-cdn.com/v4/letter/v/58956e/32.png) [@VFB](https://community.f5.com/u/VFB)\
**Post date:** [April 21, 2016, 7:51pm UTC](https://community.f5.com/t/irule-rewrite-question/2170/2 "2016-04-21T19:51:37Z")

</div>

You’d have to write iRules with 301 redirects. for each domain

---

<div class="post-metadata">

**Author:** ![VernonWells](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@VernonWells](https://community.f5.com/u/VernonWells)\
**Post date:** [April 21, 2016, 9:05pm UTC](https://community.f5.com/t/irule-rewrite-question/2170/3 "2016-04-21T21:05:46Z")

</div>

Do you want to use an explicit redirect (as ~~@VFB~~ mentions) or perform a transparent rewrite?

As usual, this is best controlled using a [class match](https://devcentral.f5.com/wiki/iRules.class.ashx). I’ll assume there are two conditions: 1. change host to marvel… and set the request-uri to /app/marvel (you say append, and if that’s what you really mean, it’s possible, but does require some changes); or 2. change host to kibana… and set the request-uri to /app/kibana. I further assume that the decision is made based on the request host. So, I’ll create a [data group](https://devcentral.f5.com/articles/v11-irules-data-group-updates) (using an internal data-group here for illustration purposes) where each data group element key is the request hostname, and the value is either “marvel” or “kibana”:

```tcl
ltm data-group internal dg-host-based-redirect { 
    records { 
        "foo.bar.com" { data "marvel" } 
        "bin.baz.net" { data "kibana" } 
        "ankle.feet.com" { data "marvel" } 
    } 
    type string 
}

```

For explicit redirect (not tested!):

```tcl
when RULE_INIT {
    set static::hbr_data_group "dg-host-based-redirect"
}

when HTTP_REQUEST {
    set m [class lookup [string tolower [HTTP::host]] $static::hbr_data_group]
    
    if { $m ne "" } {
        switch $m {
            "marvel" {
                HTTP::redirect "http://marvel.somedomain.com/app/marvel"
            }
            
            "kibana" {
                HTTP::redirect "http://kibana.somedomain.com/app/kibana"
            }
        }
    }
}

```

If you want to transparently change those (not tested!):

```tcl
when RULE_INIT {
    set static::hbr_data_group "dg-host-based-redirect"
}

when HTTP_REQUEST {
    set m [class lookup [string tolower [HTTP::host]] $static::hbr_data_group]
    
    if { $m ne "" } {
        switch $m {
            "marvel" {
                HTTP::host "marvel.somedomain.com"
                HTTP::uri "/app/marvel"
            }
            
            "kibana" {
                HTTP::host "kibana.somedomain.com"
                HTTP::uri "/app/kibana"
            }
        }
    }
}

```
