Walter_WorkAcct
Mar 11, 2022Altostratus
Nested Loop for IRules Redirects
Would anyone be willing to review this and see if it can be made more efficient or has obvious errors? I converted a .htaccess file to irules and learned I was much over the maximum character limit. I'm now trying to programmatically capture the same logic. At the same time I am finding there isn't a good way to test these except live.
when HTTP_REQUEST {
#Start Sorting By Season First
set ::season_list [fall* spring* summer* winter*]
set ::term_list [100 101 102]
set ::professor_list [smith jones smith-jones chaplain]
# Loop through the datagroup line by line.
foreach season $::season_list {
# Log the current line.
log local0. "Current \$season: $season"
# Compare the element against the string.
# If the datagroup entry has a wildcard, it should be listed first in the string compare statement.
if {[HTTP::uri] starts_with $season} then {
# Found a match, so log it and break out of the foreach loop.
foreach term $::term_list {
# Log the current line.
log local0. "Current \$term: $term"
# Compare the element against the string.
# If the datagroup entry has a wildcard, it should be listed first in the string compare statement.
if {[HTTP::uri] contains $term} then {
# Found a match, so log it and break out of the foreach loop.
foreach professor $::professor_list {
# Log the current line.
log local0. "Current \$professor: $professor"
# Compare the element against the string.
# If the datagroup entry has a wildcard, it should be listed first in the string compare statement.
if {[HTTP::uri] contains $professor} then {
# Found a match, so log it and break out of the foreach loop.
if {[HTTP::uri] ends_with ".html"} then {
HTTP::respond 301 Location "https://[HTTP::host]/archive/substr[HTTP::uri] 5].pdf" content "" noserver return
}
if {[HTTP::uri] ends_with ".htm"} then {
HTTP::respond 301 Location "https://[HTTP::host]/archive/substr[HTTP::uri] 4].pdf" content "" noserver return
}
}
else{
HHTTP::respond 301 Location "https://[HTTP::host]/archive/index.php" content "" noserver return
}
log local0. "Matched \$professor: $professor. Exiting loop."
#break after finding the professor
break
}
log local0. "Matched \$term: $term. Exiting loop."
break
}
log local0. "Matched \$season: $season. Exiting loop."
break
}
}
unset ::season_list
unset ::term_list
unset ::professor_list
}
I'm not sure I follow all the logic you're trying to achieve. Can you provide the goals of your iRule separate from the code, and if there is a full version of the iRule, can you post that (sanitized) in a gist on github or here if it'll fit?