HTTP::host

Description

Returns the value contained in the Host header of an HTTP request. This command replaces the BIG-IP 4.X variable http_host.
The Host header always contains the requested host name (which may be a Host Domain Name string or an IP address), and will also contain the requested service port whenever a non-standard port is specified (other than 80 for HTTP, other than 443 for HTTPS). When present, the non-standard port is appended to the requsted name as a numeric string with a colon separating the 2 values (just as it would appear in the browser’s address bar):
  • Host: host.domain.com:8080

RFC2616 (section 3.2.3) states that host header evaluation MUST be case insensitive. All modern browsers adhere to this standard so it is not necessary in most cases to handle case, but in some instances for non-browser traffic, it might be a good practice to set the Host header value to lower case before performing comparisons. This can be done using [string tolower [HTTP::host]].

Warning

This command will return the last instance of the HTTP host header if there are multiple host headers present in a single request. If all the host headers in the request need review, consider using the HTTP::header command instead.

Syntax

HTTP::host [name]

HTTP::host

  • Returns (or sets in v11.5+) the value of the Host header of an HTTP request. If multiple Host headers are present, only the value of the last header is returned. Also note that HTTP::host will only return the contents of a Host header – if the request uses an absolute form URI (e.g. RFC7230 section 5.3.2), HTTP::host will not return the host portion of the Absolute URI.

Examples

Both of the following examples redirect all requests containing the word “secure” to HTTPS. In this example, if a non-standard port is specified, the non-standard port from the Host header value will be retained and the resulting redirect will be to the same non-standard HTTPS port specifying HTTPS as the scheme):
when HTTP_REQUEST {
  if { [HTTP::uri] contains "secure"} {
    HTTP::redirect "https://[HTTP::host][HTTP::uri]"
 }
}

In this example, if a non-standard port is specified, the non-standard port from the Host header value will be removed and the resulting redirect will be to the standard HTTPS port specifying HTTPS as the scheme):
when HTTP_REQUEST {
  if { [HTTP::uri] contains "secure"} {
    HTTP::redirect "https://[getfield [HTTP::host] : 1][HTTP::uri]"
  }
}

To update the Host header value without redirecting the client to the new Host value, you can use HTTP::header replace Host “newhost.example.com”. This update will only affect the request to the pool member. The client will not see the update unless the web application uses the requested host header value to generate response headers and/or content.
when HTTP_REQUEST {

   # Check if requested host doesn't start with www.example.com
   if {not ([string tolower [HTTP::host]] starts_with "www.example.com")}{

      # Replace the host header value with newhost.example.com
      HTTP::header replace Host "newhost.example.com"
   }
}

Here is an another example of the a HTTP::host
when HTTP_REQUEST {
  if { [HTTP::host] equals"www.xyz.com"} {
    HTTP::redirect "http://www.abc.com/index.html"
  }
}