Forum Discussion

Craig_17655's avatar
Craig_17655
Icon for Nimbostratus rankNimbostratus
Aug 14, 2009

iRule for Redirrecting based upon URI

I am trying to replace some apache functionality using iRules. I have the following rules set up in apache:

 

 

 

SetHandler matrix

 

SetCluster imgserver

 

 

 

 

So basically, if the URI contains .img I send the request to a different set of servers that handles the requests.

 

 

I am looking for advise as I am new to irules and have not found many examples. I think it is something like this:

 

 

when HTTP_REQUEST {

 

set uri [HTTP::uri]

 

if { $uri contains ".img" }

 

pool testpool2

 

}

 

else {

 

pool testpool

 

}

 

 

Any help would be greatly appreciated.

5 Replies

  • Hi,

    You could also write it like the following:

      
      when HTTP_REQUEST {  
          if { [HTTP::uri] contains ".img" } {   
             pool testpool2   
          } else {   
             pool testpool   
          }  
      }  
      

    or

      
      when HTTP_REQUEST  
          if {([HTTP::host] eq "www.yourdomain.com") and ( [HTTP::uri] contains ".img" ) } {  
             pool testpool2   
          } else {   
             pool testpool   
          }  
      }  
      

    Click here to see how you can change the 2 operators to suit your needs

  • You could be more specific and check for a path ending in .img:

     

     

    [HTTP::path] ends_with ".img"

     

     

    This handles an image with or without a query string.

     

     

    Aaron
  • Through support from F5 I found that I should use HTTP:path instead of HTTP:uri

     

     

    This is why:

     

     

    The issue is that the HTTP:uri command begins parsing with the slash after the hostname, then grabs the path, then the query.

     

     

    From DevCentral:

     

     

     

    HTTP::uri

     

     

    * Returns the URI of the request. It does not include the protocol (http or https) or hostname, just the path and query string, starting with the slash after the hostname.

     

     

     

    http://devcentral.f5.com/Wiki/default.aspx/iRules/HTTP__uri.html

     

     

    Based on your example URL, "HTTP:uri" will return the entire line, as everything that follows the "?" character represents the query.

     

     

    Try this instead:

     

     

    HTTP::path

     

     

    "Returns or sets the path part of the HTTP request. The path is defined as the path and filename in a request. It does not include the query string."

     

     

    http://devcentral.f5.com/Wiki/default.aspx/iRules/HTTP__path.html