Monday, April 1, 2013

Simple F5 iRule re-direct

Anyone that's used F5's load balancers know how powerful, flexible and useful they can be. I frequently use them to perform re-directs from one domain to another. I've got a couple of iRule examples using the switch command to perform a 301 re-direct. Take a look after the jump.

Here is the iRule. 
when HTTP_REQUEST { switch [HTTP::host] { "" - "" - "" { HTTP::respond 301 Location "[HTTP::uri]" } "" { HTTP::respond 301 Location "[HTTP::uri]" } } }
This example uses a simple switch command to compare the requested host header with a list. One note about the switch statement if you aren't familiar with it; it can be used as a replacement for an if/elseif statement, and uses fewer CPU cycles.

There are several different things going on in this iRule. First, the switch statement here is used as a replacement for if and elseif. The if the host header matches any of the first three URL's it will re-direct to If the host header matches the second block it will re-direct to

You may have also noticed that in both cases it responds to a secure site. The actual iRule that this example is based off of actually re-direct each of these to the same VIP. In order to ensure that the SSL certificate is valid we have to have a wildcard certificate of *

Finally, this re-direct also preseves any URI that the client may have been trying to reach.

The second example is almost the same, but instead of re-directing based on the host header, it re-directs based on the URI. 

when HTTP_REQUEST { switch [HTTP::uri] { "/irule" - "/irules" { HTTP::respond 301 Location "" } } switch [HTTP::uri] { "/f5" { HTTP::respond 301 Location "" } } }


Post a Comment