Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

Using iruel for redirects

I just inherited a web hosting farm that is experencing issues contolling thier targeted urls within thier web application. They have been living with this issue in thier previous hosting site for 2-3 years I suggested we use the bigip to perform these targeted url redirects. We already successfully use iRules to do redirects for all of our ecommerce websites, seems like a good fit. We were all busy singing joyous songs of celebration when they dropped the bomb! They have almost 600 targeted urls. The most if/else url inspections I do currently in a single iRule is 15. So, my question is simply, what should I be thinking about if i'm going to build an iRule that has 600 url inspection lines in it.
0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Please, by all means, continue singing, and let's all think happy thoughts about the "findclass" command...
:D

I have some new findclass URI mapping tricks up my sleeve that I haven't yet made pretty enough to post on the CodeShare, and this may be just the kick I need to make that happen.

If you can please provide some specific examples of the URI's for which you need to perform redirects, I'm sure we can get you going.

The critical details are going to be the "inconsistencies" or variations re: parts of the URI that must be evaluated to choose the correct redirect target, for example::

redirect /dir2/thisfileonly to X
redirect /dir1/dir2/* to Y
redirect /dir1/<anythingelse> to Z

more later
/deb
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
This is the current rule that i am testing,

when HTTP_REQUEST {
if { [HTTP::uri] eq "/histology" and [HTTP::host] == "www.thermo.com" } {
HTTP::redirect http://www.thermo.com/com/cda/category/category_lp/0,2152,53,00.html
} else {
use pool www.thermo.com-pool_80
}
}

so basically, I will have 600 'if's' all with a different uri string to match and redirect to a specific page.

if { [HTTP::uri] eq "/histology" and [HTTP::host] == "www.thermo.com" } {
HTTP::redirect http://www.thermo.com/com/cda/category/category_lp/0,2152,53,00.html

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Well, I'm sort of trying to edge you away from the "death by 600 if's" version of the iRule you could create... but to do so, we'll need to know if the decision will always be based on the hostname and first directory, as in the example you give above, or will there be some variation in what needs to be evaluated -- different hostnames, different # of directories, etc.

/deb
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
always based on hostname and 1st directory.
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Sure, then, that's pretty straightforward:

Create a class list containing your request and redirect details in 2 columns, separated by a space -- hostname/firstdir in one, then the redirect target in the other.
class myRedirects {
"www.thermo.com/histology http://www.thermo.com/.../0,2152,53,00.html"
"www.whatever.com/dir1 http://newhost/newuri"
}
Then create this iRule an apply to the virtual server:

rule URIredirects {
when HTTP_REQUEST {
set req "[HTTP::host]/[getfield [HTTP::uri] "/" 2]"
set redir [findclass $req $::myRedirects]
if {$redir !=""}{
HTTP::redirect $redir
}
}
}


/deb
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Thats pretty sweet. Is the class list stored in memory, do i need to worry about memory consumption
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
stupid question, hpw do i create the class list. I read some doc, looks like i need the webacclerator module, which i do not have
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
oh, sorry, they are still called classes in the config, but I keep forgetting they are now called "Data Group Lists" in the GUI and doc.

You can just browse in the GUI to Local Traffic/iRules, click on the Data Group List tab, and create a new list of type "String". Add your list, one row at a time, with a space separating the host/uri and the redirect (GUI will add the double quotes for you).

Class lists are stored in the bigip.conf file, and are loaded into memory with the rest of the config. We have customers using very large class lists with simple iRules such as this with minimal performance impact.

/deb
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
thanks. is there a trick to adding the 600 or so strings, besides hand editing the conf file..?
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
You can edit the config file, in which case you'd need to just follow this format:
class myRedirects  {
"hydration.company.com http://www2.co.com/hydration.html"
"nhra.com http://www.us.company.com/nhraHome.do"
"redirect.co.com http://www.company.com/"
"thecompany.com http://www.thecompany.com/"
}
Once the conf file is saved, load into memory with a manual config reload ("bigpipe load" at the command line). Once the class is created, indivudual members can be updated on the fly in the GUI.

If you won't need to be updating it on the fly from the GUI, you can create what's called an "external class" which uses a separate text file to store the data. To do so, create a new text file in the /config filesystem (to sync automatically) in the following format:
"hydration.company.com http://www2.co.com/hydration.html",
"home.company.com http://www.us.company.com/Home.do",
"redirect.co.com http://www.company.com/",
"thecompany.com http://www.thecompany.com/",
and create a class of type "External" referencing this file. Note the trailing comma on each line, including the last. Changes to the file require a manual config reload before they take effect ("bigpipe load" at the command line).
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
here is the classi amusing for testing

class TURL_List {
"www.thermo.com/centrifugation http://www.thermo.com/cda/category/category_lp
/0,2152,53,00.html"
"www.thermo.com/histology http://www.thermo.com/com/cda/category/category_lp/
0,2152,211,00.html"
}

here is the irule

when HTTP_REQUEST {
set req "[HTTP::host]/[getfield [HTTP::uri] "/" 2]"
set redir [findclass $req $::TURL_List]
if {$redir !=""}{
HTTP::redirect $redir
}
}

when i hit www.thermo.com, i get this malformed http request

http://www.thermo.com/histology%20http://www.thermo.com/com/cda/category/category_lp/0,2152,211,00.html
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
ah, yes, I omitted the separator character from the findclass command. Without it, the entire row is returned; with it, only the portion after the first separator is returned.

This should work better:
rule URIredirects {
when HTTP_REQUEST {
set req "[HTTP::host]/[getfield [HTTP::uri] "/" 2]"
set redir [findclass $req $::myRedirects " "]
if {$redir !=""}{
log local0. "Redirecting request for [HTTP::host][HTTP::uri] to $redir"
HTTP::redirect $redir
}
}
}
I also added a log line to assist in troubleshooting that logs the redirect value -- once you've got it working, delete that for production use.

/d
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Works like a charm. Thanks for all your help. We can re-commence the singing of songs of joy!
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Posted By deb on 05/02/2007 12:49 PM

oh, sorry, they are still called classes in the config, but I keep forgetting they are now called "Data Group Lists" in the GUI and doc.

You can just browse in the GUI to Local Traffic/iRules, click on the Data Group List tab, and create a new list of type "String". Add your list, one row at a time, with a space separating the host/uri and the redirect (GUI will add the double quotes for you).

Class lists are stored in the bigip.conf file, and are loaded into memory with the rest of the config. We have customers using very large class lists with simple iRules such as this with minimal performance impact.

/deb


wow.. awesome post.. wish this was documented somewhere other than in the middle of a random thread
0