Forum Discussion
ShinRyuX
Nimbostratus
Mar 29, 2016Any way to shorten this switch case iRule?
Here is my iRule.
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::path]] {
"/dir1" -
"/dir1/" {
HTTP::respond 301 Location "http://www.domain1.com"
...
VernonWells
Employee
Mar 30, 2016I believe that the data group answer is the best one if you are in fact not performing globbing. Here is how I would do it (though not tested!):
tmsh create ltm data-group internal redirector-base type string { records add { \
"/file1.html" { data "http://www.newdomain.com/en-US/destination1" } \
"/dir1" { data "http://www.newdomain.com/en-US/destination2" } \
"/dir1/" { data "http://www.newdomain.com/en-US/destination2" } \
... etc ...
} }
(You could -- and probably should -- do this with an imported external data-group, but I use an internal group for illustration).
Then the rule:
when HTTP_REQUEST {
set base [class lookup "[HTTP::path]" redirector-base]
if { $base ne "" } {
Extract URL parameters
set urlPath "[string tolower [HTTP::path]]?"
if {[string tolower [HTTP::uri]] contains "?"} {
set newUri [string map -nocase [list $urlPath "?"] [HTTP::uri]]
}
else {
set newUri ""
}
HTTP::respond 301 Location "$base$newURI"
}
}
Note a few things:
- This will not work if you need to employ globbing (though, strictly speaking, you could iterate through a list of glob matchers that are keys in a data-group, but that may be [considerably] less efficient than the switch);
- I moved the
computation inside of the conditional. If there is no match, there's no point in performing the calculation;$newURI - You use a pattern that I very commonly see, namely:
. While it is true that some filesystems are case-insensitive, RFC 2616 states that everything but the scheme (i.e., the leading 'http' in this case) and the hostname should be treated in a case-sensitive manner. See RFC 2616 3.2.3 [1]. As a practical matter, "flattening" the case of the path costs CPU cycles and I believe it to be of no practical value, especially if users are not typically typing in the uri-path part, but rather they are getting to these locations via hyperlinks.[string tolower [HTTP::path]]
[1] Specifically:
When comparing two URIs to decide if they match or not, a client
SHOULD use a case-sensitive octet-by-octet comparison of the entire
URIs, with these exceptions:
- A port that is empty or not given is equivalent to the default
port for that URI-reference;
- Comparisons of host names MUST be case-insensitive;
- Comparisons of scheme names MUST be case-insensitive;
- An empty abs_path is equivalent to an abs_path of "/".
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects