iRule Host Header Pool

I’m new to F5 iRule scripting and I’m looking to simplify the following so we don’t have to edit the iRule every time we add a new site and pool.

Current

when HTTP_REQUEST { if { [HTTP::host] eq “abc.123.com” } { pool abc.123.com } elseif { [HTTP::host] eq “def.123.com” } { pool def.123.com } elseif { [HTTP::host] eq “abc.456.com” } { pool abc.456.com } }

I would like to simplify this script so that what ever comes in is sent to the pool with out defining each one.

The pool name will always match the site. Example site: abc.123.com = pool: abc.123.com

Not sure how to write the script

when HTTP_REQUEST { if { [HTTP::host] eq “*.123.com” } { pool .123.com } elseif { [HTTP::host] eq ".456.com" } { pool *.456.com } }

This is quick-and-dirty, and doesn’t allow for the virtual server receiving a request with an invalid host header:

when HTTP_REQUEST {
  pool [HTTP::host]
}

If you want to get fancier, create a data group (“known_hosts”, for example) whose entries are valid host names (and for which you’ve already created the corresponding pool).

when HTTP_REQUEST {
  if { [class match [HTTP::host] equals known_hosts] } {
    pool [HTTP::host]
  } else {
     take appropriate action
  }
}