What could you do with your code in 20 Lines or Less? That's the question I ask (almost) every week for the devcentral community, and every week I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head.

This week we’ve got more cool iRules toys to show off. That’s not the impressive part, the impressive part is that only two of the three examples are from hoolio.  I know, right?  Joking aside, a huge thank you to hoolio for his astounding contributions to the community, and to everyone else that’s out there posting questions, answers and examples of what iRules, iControl and all the other F5 technologies can do.

Okay, love-fest out of the way, let’s iRule…

Nested URI Rewriting

http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&postid=60448&view=topic

In this example hoolio shows how you can use a switch statement to simplify performing a couple of URI rewrites based on the hostname.  The example is a clean way to achieve something that can quickly get messy and hard to debug when using if/else chains.

 when HTTP_REQUEST { 
switch [string tolower [HTTP::host]] {
"keyaccount.company.com" {
# Rewrite / to /path/path/index.html
if {[HTTP::path] eq "/"}{
HTTP::uri "/path/path/index.html"
}

# Rewrite host header to keyaccount.othercompany.com
HTTP::header replace Host "keyaccount.othercompany.com"

# Use pool1
pool pool1
}
"keyaccount.company.com" {
# Rewrite / to /path/path/index.html
if {[HTTP::path] eq "/"}{
HTTP::uri "/path/path/index.html"
}

# Rewrite host header to keyaccount.othercompany.com
HTTP::header replace Host "keyaccount.othercompany.com"

# Use pool1
pool pool1
}
default {
# Take some default action for other host header values?
}
}
}

 

Partial Hostname Rewrite

http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&postid=60325&view=topic

User cmbhatt chimed in with a solid response to a request looking for hostname re-writing.  Rather than perform some more complex splitting, lreplacing and joining he makes use of the string map command to reduce the complexity and number of steps. This one’s another good example of making something that’s easy to read, useable and portable.

 when HTTP_REQUEST {  
if { [matchclass [string to lower [HTTP::header "User-Agent"]] contains $::useragent_list ] } {
HTTP::redirect "http://[string map -nocase {"www." "m." } [HTTP::host]][HTTP::uri]"
} else {
HTTP::redirect "http://[string map -nocase {"www." "www2." } [HTTP::host]][HTTP::uri]"
}
}

 

IP based login control via iRules

http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&postid=60333&view=topic

Hoolio’s back (shocking) with another awesome example.  His response to player’s request for some user access control guidance resulted in a good look at how you can use a simple iRule to make quick work of login restrictions.  This is a great way to allow only certain people access to given areas of a site.

when HTTP_REQUEST { 

    # Check requested path
    if {[string tolower [URI::decode [HTTP::path]]] eq "/web/admin.aspx"}{ 

       # Check login parameter value
       if {[URI::query [HTTP::uri] "login"] eq "1"}{ 

          # Request to restricted resource.  Check if client IP is not 1.1.1.1
          if {not ([IP::addr [IP::client_addr] equals 1.1.1.1])}{ 

             # Take some action to prevent request? 

             # Rewrite login=1 to login=2 
             #HTTP::uri [string map {login=1 login=2} [HTTP::uri]]

         # Redirect client to rewritten URI? 
             #HTTP::redirect [string map {login=1 login=2} [HTTP::uri]]
          } 
       } 
    }
}

There they are for this week, three more examples of iRule foo in less than 21 lines of code. Check out more 20 Lines or Less posts here.

#Colin