Forum Discussion

cames1_233757's avatar
cames1_233757
Icon for Nimbostratus rankNimbostratus
Feb 02, 2016

Wildcard for Host Subdomains inside a Switch?

Is there a way to use wildcards to match any subdomain for Host entries inside a switch?

So, for example:

 

switch -glob [string tolower [HTTP::host]] {
"*.domain.com" { Do Something }

 

When I try test.domain.com, it just matches my default switch entry.

Thanks!

5 Replies

  • mo_99289's avatar
    mo_99289
    Historic F5 Account

    hope this could help

     

    when HTTP_REQUEST {
        log local0. "Host is [string tolower [HTTP::host]]"
        switch -regexp [string tolower [HTTP::host]] {
           .*\.a.com {
              log local0. "*.a.com"
           }
           .*\.b.com {
              log local0. "*.b.com"
           }
           default {
              log local0. "default domain"
           }
    }
    

     

    simple test:

    for host in web.a.com web.b.com web.c.coom; do curl -o /dev/null -H "Host: $host" http://10.2.22.183/; done

    ltm log:

     

    Feb  2 17:28:00 ve3 info tmm1[32273]: Rule /Common/irule_http_request : Host is web.a.com
    Feb  2 17:28:00 ve3 info tmm1[32273]: Rule /Common/irule_http_request : *.a.com
    Feb  2 17:28:00 ve3 info tmm[32273]: Rule /Common/irule_http_request : Host is web.b.com
    Feb  2 17:28:00 ve3 info tmm[32273]: Rule /Common/irule_http_request : *.b.com
    Feb  2 17:28:00 ve3 info tmm1[32273]: Rule /Common/irule_http_request : Host is web.c.coom
    Feb  2 17:28:00 ve3 info tmm1[32273]: Rule /Common/irule_http_request : default domain
    

     

  • Thanks for the advice - I will give that a try!

    Just to check I understand, are the "log.local0." entries just so I can check the logic is working in the LTM log?

    So, if all looks good on testing, the actual rule would look something like this?

     

    when HTTP_REQUEST {
        switch -regexp [string tolower [HTTP::host]] {
            .*\.a.com {
                do something
             }
            .*\.b.com {
                do something
             }
            default {
                do something
            }
    }
    

     

    Thanks again!

  • Hi Cames1,

    your [switch -glob] code looks good and the HOST test.domain.com should normaly match the *.domain.com condition. Chaning to [switch -regexp] is not required for this simple condition, it would just put some heavy CPU load on your device. The performance impact of [switch -regexp] is realy impressive and should be avoided if possible...

    My bet is, that you've saved and tested the iRule without opening and closing your browser (aka. establishing new TCP connections). Keep in mind that any changes to iRules would just affect any new TCP connections.

    To see whats going on behind the scenes, you may also want to add some [log] statements...

     

    log -noname local0.debug "Before switch: [string tolower [HTTP::host]]"
    switch -glob -- [string tolower [HTTP::host]] {
        "*.domain.com" {
            log -noname local0.debug "*.domain.com switch!"
             Do Something
            }
        default {
            log -noname local0.debug "default switch!"
             Do Something
            }
    }
    

     

    Cheers, Kai

  • Kai - You are absolutely correct, it is working now. I should have run better testing.

     

    Thanks all for your help - It is good to learn more about the log option too!