Skip to content

Instantly share code, notes, and snippets.

@lrhazi
Last active December 15, 2015 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrhazi/89b466ed2089cb19e6f9 to your computer and use it in GitHub Desktop.
Save lrhazi/89b466ed2089cb19e6f9 to your computer and use it in GitHub Desktop.
F5 BigIP iRule to manage HTTP redirects using a simple data group.
when RULE_INIT {
# set DEBUG to 1 for debug log output
set static::DEBUG 1
set static::class_name fatwire_redirects_class
set static::sepa ","
}
when HTTP_REQUEST {
set r_host [HTTP::header "Host"]
set r_uri [HTTP::uri]
if { [class search $static::class_name starts_with $r_host ] } {
set id [class startsearch $static::class_name]
while {[class anymore $static::class_name $id]}{
set line [class nextelement -name $static::class_name $id]
if {$static::DEBUG == 1 } {log local0. "Line: $line"}
if {[catch {foreach {host uri target http_code} [split $line $static::sepa] {break}} errmsg]} {
log local0. "Error parsing line - $errmsg : $line"
} else {
#TODO: Test all 4 arguments.
if {$static::DEBUG == 1 } {log local0. "Host: $host URI: $uri Target: $target Code: $http_code"}
if { [string match $host $r_host] } {
if { [string match $uri $r_uri] } {
if {[catch {eval "set target_expaned $target"} errmsg]} {
log local0. "Error expanding target - $errmsg : $target"
set target_expaned $target
}
if {$static::DEBUG == 1 } {log local0. "Redirecting $r_host $r_uri to: $target_expaned with code: $http_code"}
if {[catch {HTTP::respond $http_code Location $target_expaned} errmsg]} {
log local0. "Error executing the redirect - $errmsg : $target_expaned"
}
break
}
}
}
}
class donesearch $static::class_name $id
} else {
if {$static::DEBUG == 1 } {log local0. "Host not found in data group."}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment