Forum Discussion

Brent_Sachnoff_'s avatar
Brent_Sachnoff_
Icon for Nimbostratus rankNimbostratus
May 24, 2006

funny things happen with basic iRule

Hi. I have this iRule that appeared to work the first couple of times around and now only works for the 1,2 and last case within the if clause. I'm sure there is a better way to write it but I'm still learning:

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] starts_with "a.company.com" }

 

{ HTTP::redirect "https://www.company.com/123/Login.html" }

 

elseif { [HTTP::host] starts_with "company.com" }

 

{ HTTP::redirect "https://www.company.com/123/Login.html" }

 

elseif { [HTTP::uri] equals "/" }

 

{ HTTP::redirect "https://[HTTP::host]/123/Login.html" }

 

elseif { [HTTP::uri] equals "/Login.html" }

 

{ HTTP::redirect "https://[HTTP::host]/123/Login.html" }

 

elseif { [HTTP::uri] equals "/456/index.html" }

 

{ HTTP::redirect "https://[HTTP::host]/123/Login.html" }

 

elseif { [HTTP::uri] equals "/mobileLogin.html" }

 

{ HTTP::redirect "https://[HTTP::host]/123/Login.html" }

 

else { HTTP::redirect https://[HTTP::host][HTTP::uri] }

 

}

 

 

Any reason for this not to work for all the clauses above?

 

 

Thanks!

5 Replies

  • As long as the rule can compile (due to the elseif's on new lines), then it should functionaly work. I would add some logging in to output the values of the HTTP::host and HTTP::uri variables and then a log statement for the condition that matched. starts_with and equals are case-sensitive. Maybe that could be the issue. No way to tell without some input data.

    BTW, in the future, I'd recommend avoiding elseif's and else's on newlines. That isn't supported by TCL and our implementation of that has some problems under certain situations such as trailing spaces on the previous lines).

    I'd start with something like this:

    when HTTP_REQUEST {
      log local0. "Host: [HTTP::host], URI: [HTTP::uri]"
      if { [HTTP::host] starts_with "a.company.com" } {
        log local0. "host matched a.company.com"
        HTTP::redirect "https://www.company.com/123/Login.html"
      } elseif { [HTTP::host] starts_with "company.com" } {
        log local0. "host matched company.com"
        HTTP::redirect "https://www.company.com/123/Login.html"
      } elseif { [HTTP::uri] equals "/" } {
        log local0. "URI matched '/'"
        HTTP::redirect "https://[HTTP::host]/123/Login.html"
      } elseif { [HTTP::uri] equals "/Login.html" } {
        log local0. "URI matched /Login.html"
        HTTP::redirect "https://[HTTP::host]/123/Login.html"
      } elseif { [HTTP::uri] equals "/456/index.html" } {
        log local0. "URI matched /456/index.html"
        HTTP::redirect "https://[HTTP::host]/123/Login.html"
      } elseif { [HTTP::uri] equals "/mobileLogin.html" } {
        log local0. "URI matched /mobileLogin.html"
        HTTP::redirect "https://[HTTP::host]/123/Login.html"
      } else {
        log local0. "URI match not found, using default case"
        HTTP::redirect https://[HTTP::host][HTTP::uri]
      }
    }

    Then take a look at the /var/log/ltm file on the BIG-IP to find out why your cases don't work.

    -Joe
  • Joe,

     

     

    Thanks for the help on this. I have implemented your suggestions and noticed the same behavior. The weird thing is that the logging never shows anything hitting the 2 cases even though I've tried multiple times. The logging does show hits on the other cases. I feel as if the BigIP is not proxying for those addresses. My browser is just timing out. If I type in

     

    junk we get this:

     

     

    May 24 11:56:01 tmm tmm[747]: Rule beta-redirection : Host: beta.company.com, URI: /junk

     

    May 24 11:56:01 tmm tmm[747]: Rule beta-redirection : URI match not found, using default case all

     

     

    And an error 400. Those other two don't even respond nor do I get a 400. Instead I receive the standard windows page can not be displayed (internet name not resolved). Sounds like I might need to contact F5.

     

     

    As for being case sensitive, are there other commands that are not case sensitive that accomplish what I am looking to do?

     

     

    Once again, thanks for the input on this.
  • So are you saying that for some URI's you are getting the first logging line and for some URI's you are not? That doesn't make any sense. You should be hitting the HTTP_REQUEST event for each HTTP request and the first log command should always execute.

    As for case sensitivity, you'll have to convert the strings to lowercase with the "string tolower" command

    set newUri [string tolower [HTTP::uri]]
    if { $newUri starts_with ... } {
     ...
    }

    One other question. I'm assuming that this rule is not redirecting to itself with the default case. If so, you'll get a circular loop on your last HTTP::redirect.

    -Joe
  • That is exactly what I am saying. Sometimes when I hit the url it logs and when I try to hit the 2 cases it never logs anything. Regardless, it should always log something for me. Very weird.

     

     

  • You might want to give the box a kick by running bigstart restart when you are able to (this will restart the processes on the BIG-IP and drop all existing connections).

     

     

    If the problem recurs, you might want to open a support case.

     

     

    Aaron