HTTP to HTTPS redirect by vs name

Problem this snippet solves:

Redirect HTTP requests to HTTPS using the virtual server name to avoid certificate mismatch warnings

iRule Methodology

This iRule example redirects all requests to the virtual server to HTTPS. The hostname is parsed from the virtual server name. The parsing takes up to the first underscore in the virtual server name and appends the originally requested URI.

Configuration Requirements

Any virtual server that this iRule is used on must be named with the fully qualified domain name that DNS resolves to the virtual server IP address. For example, www.example.com would need to resolve to the HTTPS virtual server IP address (or the IP address that is NAT'd to the virtual server IP address).

A caveat is that you can't rename virtual servers via the GUI. You could edit the bigip.conf and reload the configuration if you need to alter the virtual server name.

How to use this snippet:

Implementation Details

This iRule requires LTM v10. or higher.

# A virtual server named with the FQDN
virtual www.example.com_http_vs {
   destination 10.1.0.15:80
   ip protocol tcp
   rules http_to_https_redirect_by_vs_name_rule
   profiles {
      http {}
      tcp {}
   }
}

# Another virtual server named with the FQDN
virtual www2.example.com_http_vs {
   destination 10.1.0.16:80
   ip protocol tcp
   rules http_to_https_redirect_by_vs_name_rule
   profiles {
      http {}
      tcp {}
   }
}

Code :

when HTTP_REQUEST {

   # Use getfield to parse the virtual server name up to the first underscore
   # Redirect the HTTP request to https:// using that parsed name
   log local0. "Redirecting to https://[getfield [virtual name] _ 1][HTTP::uri]"
   HTTP::redirect "https://[getfield [virtual name] _ 1][HTTP::uri]"
}
Published Mar 18, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment