Alan_Evans_1020
May 24, 2011Nimbostratus
iRule to redirect to new name after given time
Our customer is going through a name change and we will need to redirect from oldname to newname starting on a given date. I realize I could just apply the iRule at the right time BUT why not automate it?
There are at least a dozen sites that will need redirects and I imagine after the cutover date there will be a bunch of "oops we missed xyz.example.com" requests so rather than writing an iRule for each case and applying it only to the necessary VS I would like to write one iRule that handles all the VS.
There is an example that uses a data group and space separated old/new values but I can't imagine that the regex processing in that setup makes it particularly efficient. I figured an array would be a clever way to do this instead. Just create an array with hosts for keys and new URLs for values. Check the array for the existence of the current host and redirect if necessary. This also affords me the ability to handle http/https redirects. Many of the VS have a general rule that redirects HTTPtoHTTPS already so if I insert the name change rule ahead of those I won't end up with multiple redirects.
We're running 10.2.something I forget exactly what point/hotfix on 1600s.
Here's what I have that validates when I submit the iRule to the web UI but does not work when I try to hit it. I am having trouble w/ TCL syntax, exprs, etc etc etc.
when RULE_INIT {
array set static::redirects {
app.example.com https://app.newexample.com/
www.example.com http://www.example.com/
}
Epoch time for 6/1/2011 00:00:00
set static::cutovertime 1306900800
}
when HTTP_REQUEST {
if { ([clock seconds] > $static::cutovertime) && ([info exists $static::redirects([string tolower [HTTP::host]])]) } {
log local0. "Redirecting to: [redirects([HTTP::host])][HTTP::uri]"
HTTP::redirect "[redirects([HTTP::host])][HTTP::uri]"
}
}
Can anyone help w/ my syntax? I write Bash/POSIX/Perl daily but I just can't seem to get my head around TCL yet.
Regards,
-Alan