For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

John_Heyer_1508's avatar
John_Heyer_1508
Icon for Cirrostratus rankCirrostratus
Jul 13, 2015

How to get domain name in iRule?

I have a redirect rule that I need to apply to about 30 different FQDNs:

foo.domain1.com/XYZ -> bar.domain1.com/XYZ
foo.domain2.com/XYZ -> bar.domain2.com/XYZ
foo.domain3.com/XYZ -> bar.domain3.com/XYZ

I could do this explicitly in the irule or enter all domains using a data group list, but was hoping there would be a way to get the domain automatically. Something like:

when HTTP_REQUEST {
   if { [HTTP::host] starts_with "foo" } {
     HTTP::redirect https://bar.[DOMAIN]/[HTTP::uri]
   }
}

Anyone had to do this, and what's the best way?

5 Replies

  • Hi,

    If you do not need to treat the domain name, you could do this with the "string map" statement.
    when HTTP_REQUEST {
       if { [HTTP::host] starts_with "foo." } {
            HTTP::redirect "https://[string map -nocase {foo. bar.} [HTTP::host]][HTTP::uri]"
       }
    }
    

    otherwise, you could treat it with "string range" and "string first" statement.

    when HTTP_REQUEST {
       if { [HTTP::host] starts_with "foo." } {
            set dotdomain [string range [HTTP::host] [string first . [HTTP::host]] end]
            set host bar$dotdomain
            set url https://$host[HTTP::uri]
            log local0. "Domain $dotdomain | Host: $host | Url: $url"
            HTTP::redirect $url
       }
    }
    

    It's just a tip, I hope it helps you. Regards

  • Vernon_97235's avatar
    Vernon_97235
    Historic F5 Account

    Certainly. Firstly, I assume you want to make sure that you match "foo" only when it is the complete name leaf, and outside of that, you will substitute any domain name which contains that leaf:

    when HTTP_REQUEST {
        if { [HTTP::host] starts_with "foo." } {
            HTTP::redirect "https://bar.[string range [HTTP::host] 4 end][HTTP::uri]
        }
    }
    

    The

    4
    in the
    string range
    command is the length of the leaf name and its following dot (
    foo.
    , in this example). Notice, as well, that the leading slash (
    /
    ) is not needed before
    HTTP::uri
    (although, strictly speaking, a user-agent could pass the protocol and/or host part in the Request-URI, but that's an entirely different problem).

    • John_Heyer_1508's avatar
      John_Heyer_1508
      Icon for Cirrostratus rankCirrostratus
      That's another nice solution. Really all depends on what's more readible. And yes, realized the URI will always start with a / so no need to have an extra one in there.
  • Certainly. Firstly, I assume you want to make sure that you match "foo" only when it is the complete name leaf, and outside of that, you will substitute any domain name which contains that leaf:

    when HTTP_REQUEST {
        if { [HTTP::host] starts_with "foo." } {
            HTTP::redirect "https://bar.[string range [HTTP::host] 4 end][HTTP::uri]
        }
    }
    

    The

    4
    in the
    string range
    command is the length of the leaf name and its following dot (
    foo.
    , in this example). Notice, as well, that the leading slash (
    /
    ) is not needed before
    HTTP::uri
    (although, strictly speaking, a user-agent could pass the protocol and/or host part in the Request-URI, but that's an entirely different problem).

    • John_Heyer_1508's avatar
      John_Heyer_1508
      Icon for Cirrostratus rankCirrostratus
      That's another nice solution. Really all depends on what's more readible. And yes, realized the URI will always start with a / so no need to have an extra one in there.