Forum Discussion
Martin_Kaiser_1
Nimbostratus
Oct 20, 2005using getfield to split up URIs
Hi there,
can anyone of you show me the correct syntax for using the function getfield in an iRule?
What I want to do is split up an URI like "/webstart/portal/customer_id/some/more/fie...
Oct 24, 2005
You can get rid of the regexp_result variable by removing the brackets around the regexp call
when LB_FAILED {
set uri [HTTP::uri]
if {$uri contains "&view="} {
regexp {view=([a-zA-Z]+)} $uri viewstring cust_id
HTTP::redirect https://some.maintenancesite.com/$cust_id
}
}
If you want to get rid of the viewstring variable as well, you could do something like the following:
when LB_FAILED {
set uri [HTTP::uri]
if {$uri contains "&view="} {
set cust_id [lindex [regexp -inline {view=([a-zA-Z]+)} $uri] 1]
HTTP::redirect https://some.maintenancesite.com/$cust_id
}
}
The "-inline" argument to regexp tells it to return a list instead of putting the results in the match args. The lindex command will extract a specified element from a list. In this case the second one (or index 1). The info on these commands are available in the TCL Reference at Sourceforge
http://tmml.sourceforge.net/doc/tcl/
Click here
The only comment I have is whether cust_id alwasy is only characters from a-z. If you want to include underscores, numbers, etc, then you can replace the a-z ranges with a word character expression
when LB_FAILED {
set uri [HTTP::uri]
if {$uri contains "&view="} {
regexp {view=(\w+)} $uri viewstring cust_id
HTTP::redirect https://some.maintenancesite.com/$cust_id
}
}
Honestly, I'm not sure which one is more efficient. Putting the values into two variables, or a 2 element temporary list and a single variable. Probably the first option is better performance wise, but I'm not sure.
One more approach you could take to this is to avoid the "contains" match and rely on the regexp return code.
when LB_FAILED {
set retcode [regexp {view=([a-zA-Z]+)} [HTTP::uri] viewstring cust_id]
if { 1 == $retcode } {
HTTP::redirect https://some.maintenancesite.com/$cust_id
}
}
or, to make it even more concice you could do the following:
when LB_FAILED {
if { 1 == [regexp {view=([a-zA-Z]+)} [HTTP::uri] viewstring cust_id] } {
HTTP::redirect https://some.maintenancesite.com/$cust_id
}
}
Try them all out and see which works better for you...
-Joe
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects
