Forum Discussion

eddie_lim_11634's avatar
eddie_lim_11634
Icon for Nimbostratus rankNimbostratus
Sep 19, 2017

Requires assistance with iRule conundrum. /abc/job, /abc/jobs, /abc/jobstatus

Hi, I was asked to setup a redirect iRule for these uri, /abc/jobs, /abc/job, /abc/jobstatus.

when HTTP_REQUEST {

  if { [HTTP::uri] starts_with "/abc/jobs"}{

   HTTP::redirect [string map {/abc/jobs /def/ghi/jobs} [HTTP::uri] ]
}
  elseif { [HTTP::uri] starts_with "/abc/jobstatus"}{

   HTTP::redirect [string map {/abc/jobstatus /def/xyz/executions} [HTTP::uri] ]
}
  elseif { [HTTP::uri] starts_with "/abc/job"}{

   HTTP::redirect [string map {/abc/job /def/qrs/logging} [HTTP::uri] ] 
}

}

The problem is when I access /abc/jobs, I get /def/qrs/loggings. When I access /abc/job, it stalls. /abc/jobstatus seems to be the only working one.

Please let me know what I did wrong. Thanks.

2 Replies

  • It's odd that abc/jobstatus seems to be the only one working, since it matches both the first and second IF tests. In other words, it matches "starts_with abc/jobs" and "starts_with abc/jobstatus."

     

    I would code the IF test in the sequence longest to shortest match. So, test for "starts_with abc/jobstatus" first, then "abc/jobs" and finally for "abc/job" to avoid the possibility of matching prematurely.

     

    Also, keep in mind that "string map" replaces all occurrences of the search string in the target string. Without seeing the whole URI, it's hard to know if the abc/job string map is replacing more than just the first part of the URI. That could explain why that particular request is not completing properly. You could use "string replace" instead of "string map" and limit the starting and ending indices on each statement to target just the beginning of the URI.

     

  • Hi,

    Use switch command instead of multiple if elseif

    when HTTP_REQUEST {        
      switch -glob -- [HTTP::path] {      
          "/abc/jobs" {
              HTTP::redirect [string map {/abc/jobs /def/ghi/jobs} [HTTP::uri] ]
          }     
          "/abc/jobstatus" {
              HTTP::redirect [string map {/abc/jobstatus /def/xyz/executions} [HTTP::uri] ]
          }    
          "/abc/job" -          
          "/abc/job/*" {
              HTTP::redirect [string map {/abc/job /def/qrs/logging} [HTTP::uri] ] 
          }          
       }
    }