on 02-Oct-2012 23:43
What could you do with your code in 20 Lines or Less? That's the question I like to ask for the DevCentral community, and every time I go looking to find cool new examples that show just how flexible and powerful iRules can be without getting in over your head. If there's one thing I learned on the road recently, traipsing around to some awesome user groups, it's that there's nearly no end to the uses for iRules in the real world. I've seen things ranging from the simplest redirect rules all the way up to some pretty gnarly complexity implementations, and everything in between. It's awesome to see iRules of many incarnations doing their thing, and it continues to remind me just how many ways people are using this technology in a myriad of ways.
To that end this week brings another set of iRules examples showcasing a few more ways in which the community is putting iRules to work. Thanks to the wicked community doing that thing that you all do, we've got Virtual to Virtual redirection, rewriting mid redirection, as well as separation of URL/URI content. Not only that, but this week examples in less than 20 lines wasn't enough of a challenge. Instead I'm delivering 3 examples in less than 10 lines of code each. It's the super minified version of the 20LoL, for those efficiency minded folks. Let's dig in:
1: when CLIENT_ACCEPTED {
2: if {[TCP::remote_port] == 80} {
3: virtual HTTP_virtual
4: }
5: }
1: when HTTP_REQUEST {
2: switch -glob [string tolower [HTTP::host][HTTP::uri]] {
3: "some.host.com/singleuri*" {
4: HTTP::respond 301 Location "https://some.other.host.com[string map {"/singleuri" "/anotheruri"} [HTTP::uri]]"
5: }
6: }
7: }
1: when HTTP_REQUEST {
2: set host [HTTP::host]
3: set uri_list [split [string tolower [HTTP::path]] /]
4: if { [lindex $uri_list 2] equals "admin"} {
5: HTTP::respond 302 Location "https://$host/user"
6: }
7: }