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/fields" and extract the field customer_id, that is always in the third position. The value should be stored in a variable that can be used again later for dynamically choosing pools or redirects in case of LB_FAILED.
Thanks in advance!
Regards,
Martin
- Martin,
when HTTP_REQUEST { set cust_id [getfield [HTTP::uri] "/" 4]] if { $cust_id equals "company1" } { pool company1_pool } elseif { $cust_id equals "company2" } { pool company2_pool } }
when HTTP_REQUEST { set cust_id [string tolower [getfield [HTTP::uri] "/" 4]]] if { $cust_id equals "company1" } { pool company1_pool } elseif { $cust_id equals "company2" } { pool company2_pool } }
- Martin_Kaiser_1
Nimbostratus
Joe, - Martin_Kaiser_1
Nimbostratus
As I am beginning to realize the opportunities, would it also be possible to do something like that:when HTTP_REQUEST { set cust_id [getfield [HTTP::uri] "/" 4]] pool $cust_id_pool }
- You suppose wrong, well almost...
when HTTP_REQUEST { set cust_id [getfield [HTTP::uri] "/" 4]] pool ${cust_id}_pool }
- Martin_Kaiser_1
Nimbostratus
Hi again, - Martin_Kaiser_1
Nimbostratus
I just found the answer for myself:when LB_FAILED { set uri [HTTP::uri] if {$uri contains "&view="} { set regexp_result [regexp {view=([a-zA-Z]+)} $uri viewstring cust_id] HTTP::redirect https://some.maintenancesite.com/$cust_id } }
- 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 } }
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 } }
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 } }
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 } }
when LB_FAILED { if { 1 == [regexp {view=([a-zA-Z]+)} [HTTP::uri] viewstring cust_id] } { HTTP::redirect https://some.maintenancesite.com/$cust_id } }
- Martin_Kaiser_1
Nimbostratus
joe, - unRuleY_95363Historic F5 AccountWe also have some URI convenience functions like URI::query. So you can simply write a rule like this:
when LB_FAILED { set cust_id [URI::query [HTTP::uri] "view"] if {$cust_id ne ""} { HTTP::redirect https://some.maintenancesite.com/$cust_id } }
- unRuleY_95363Historic F5 AccountBTW, Joe's last example is actually best as it completely avoids using any variables. This will actually yield the best performance, but if you are not concerned about that then I would go with the readability of using some variables.
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