iRule Recipe 1: Single URL Explicit Redirect
Series Introduction
Let's face it: there aren't many people out there who have extensive experience with Tcl. Since iRules is a Tcl dialect, that means that finding a solid iRules solution can be ...
Updated Jun 06, 2023
Version 2.0VernonWells
Employee
Joined August 23, 2012
VernonWells
Oct 12, 2018Employee
HTTP::host
retrieves the value of the Host header, while the Request-Target path is part of the Request-Target in the HTTP Request Start-Line. Consider a request in your browser URL bar:
http://site.com/newpath
This would produce an HTTP Request message similar to the following (I omit a number of likely headers that aren't relevant to this example):
GET /newpath HTTP/1.1
Host: site.com
Notice that the path element (i.e.,
/newpath
) is in the Request-Line. To extract that, you should use HTTP::path
. Thus, to match the URL above, you would do this:
when HTTP_REQUEST {
switch [string tolower [HTTP::host]] {
"site.com" -
"www.site.com" {
if { [HTTP::path] eq "/newpath" } {
HTTP::respond 301 Location "https://www.site2.com/newpath"
}
}
}
}
I use
switch
to reduce the number of evaluations of HTTP::host
and string tolower
, but this could also be written:
if { [string tolower [HTTP::host]] eq "site.com" or [string tolower [HTTP::host]] eq "www.site.com" } { ... }
Having said all of this, I strongly recommend that you consider using a Local Traffic Policy.