Forum Discussion
IRule - combine 2 variables with an and && statment
Having issues with an irule. I'm no where an expert so please forgive my ignorance. Everything else works but the following statement (bolded is the new portion, rest is working): if { ( $redirect_to_mobile == "true" && [matchclass [$lower_uri] starts_with $group] } {
Basically I want to detect the user browser (iphone, etc.) AND detect the url they are hitting and based on that forward them to the mobile friendly site.
Complete IRule:
when HTTP_REQUEST {
Set the user-agent field to all lower case so that the data group matches are not case sensitiveset user_agent [string tolower [HTTP::header User-Agent]] set non_mobile_pool "www.ddd.com" set mobile_url "http://m.ddd.com" set redirect_to_mobile "false" set group $::group_web_uri_dddMobile
Determine if the user agent (all lower case) is a member of the data group if { [matchclass $user_agent contains $::group_web_mobile_user_agents] } { log local0. "dddMobile - User-agent ($user_agent) is a member of the group_mobile_user-agents data group" set redirect_to_mobile "true"
Check to see if it is an Android tablet. If it is, do not send it to the mobile site
if { ( $user_agent contains "android" ) && not ( $user_agent contains "mobile" ) } {
log local0. "dddMobile - User-agent ($user_agent) contains Android but is not Mobile"
set redirect_to_mobile "false"
}
} else { log local0. "dddMobile - User-agent ($user_agent) is NOT a member of the group_mobile_user-agents data group" set redirect_to_mobile "false" }
Now send the request to the appropriate destination based upon the value of the redirect_to_mobile variable if { ( $redirect_to_mobile == "true" && [matchclass [$lower_uri] starts_with $group] } { HTTP::redirect "$mobile_url[HTTP::uri]"
} else { pool $non_mobile_pool }
}
13 Replies
- Greg_33932
Nimbostratus
sorry for the formatting, Firefox and IE will not allow me to correct it on my work computer... Get errors. - IheartF5_45022
Nacreous
The only thing I can see immediately is that you did not have $lower_uri defined. Here is iRule reformatted using "class match" rather than matchclass (which has been deprecated).
when CLIENT_ACCEPTED { set group "group_web_uri_dddMobile" set group_web_mobile_user_agents "group_web_mobile_user_agents" } when HTTP_REQUEST { Set the user-agent field to all lower case so that the data group matches are not case sensitive set user_agent [string tolower [HTTP::header User-Agent]] set non_mobile_pool "www.ddd.com" set mobile_url "http://m.ddd.com" set redirect_to_mobile "false" set group $::group_web_uri_dddMobile Determine if the user agent (all lower case) is a member of the data group if { [class match $user_agent contains $group_web_mobile_user_agents] } { log local0. "dddMobile - User-agent ($user_agent) is a member of the group_mobile_user-agents data group" set redirect_to_mobile "true" Check to see if it is an Android tablet. If it is, do not send it to the mobile site if { ( $user_agent contains "android" ) && not ( $user_agent contains "mobile" ) } { log local0. "dddMobile - User-agent ($user_agent) contains Android but is not Mobile" set redirect_to_mobile "false" } } else { log local0. "dddMobile - User-agent ($user_agent) is NOT a member of the group_mobile_user-agents data group" set redirect_to_mobile "false" } Now send the request to the appropriate destination based upon the value of the redirect_to_mobile variable if { ( $redirect_to_mobile == "true" && [class match [string tolower [HTTP::uri]] starts_with $group] } { HTTP::redirect "$mobile_url[HTTP::uri]" } else { pool $non_mobile_pool } } - Greg_33932
Nimbostratus
Thanks for the response. Doing some testing now. Thanks!
- Greg_33932
Nimbostratus
Looks good and thanks again, Sorry for the late replay, been working on a new IBM POC application for our company.
Looks like I also need to add an additional statement to allow 404 responses from our internal servers. I've put together the following and seem to have a syntax error or I'm using the wrong command. Any advice?
Based on my searches I've put together the below string. I'll place that as the fist irule to fire (after the "set" variables) and it seems to fire in the right order, just on mobile devices it does not load anything so so its not happy. Thanks again for the assistance!
My issue seems to be this part of the statement:
{if [HTTP::status] == "404" }The combined statements I'm trying to use is:
if { ( $redirect_to_mobile == "true") && {if [HTTP::status] == "404" } } { pool $non_mobile_pool } - Greg_33932
Nimbostratus
Full code combined:
when CLIENT_ACCEPTED { set group "group_web_uri_Mobile" set group_web_mobile_user_agents "group_mobile_user_agents" } when HTTP_REQUEST { Set the user-agent field to all lower case so that the data group matches are not case sensitive set user_agent [string tolower [HTTP::header User-Agent]] set non_mobile_pool "qa.bbb.com" set mobile_url "http://mobileqa.bbb.com" set redirect_to_mobile "false" set group $::group_uri_Mobile Determine if the user agent (all lower case) is a member of the data group if { [class match $user_agent contains $group_web_mobile_user_agents] } { log local0. "Mobile - User-agent ($user_agent) is a member of the group_mobile_user-agents data group" set redirect_to_mobile "true" Check to see if it is an Android tablet. If it is, do not send it to the mobile site if { ( $user_agent contains "android" ) && not( $user_agent contains "mobile" ) } { log local0. "Mobile - User-agent ($user_agent) contains Android but is not Mobile" set redirect_to_mobile "false" } } else { log local0. "Mobile - User-agent ($user_agent) is NOT a member of the group_mobile_user-agents data group" set redirect_to_mobile "false" } if { ( $redirect_to_mobile == "true") && {if [HTTP::status] == "404" } } { pool $non_mobile_pool } Now send the request to the appropriate destination based upon the value of the redirect_to_mobile variable if { ( $redirect_to_mobile == "true" && [class match [string tolower [HTTP::uri]] starts_with $group] ) } { HTTP::redirect "$mobile_url[HTTP::uri]" } else { pool $non_mobile_pool } } - IheartF5_45022
Nacreous
Hi there use either
{if [HTTP::status] eq "404" }or
{if [HTTP::status] == 404 }I think the 2nd one slightly more efficient (or I could be making that up).
- Greg_33932
Nimbostratus
Thanks for the reply, for some reason the page still does render on the handheld device with this line in.
if { ( $redirect_to_mobile == "true") && {if [HTTP::status] == "404" } } { pool $non_mobile_poolAs soon as I comment this part out it allows the page to render. I'm basically looking to capture 404 responses and pass that on to our back end servers to handle the 404 message. I'm not sure what I'm missing in the irule logic. Thanks again for the advice!
- IheartF5_45022
Nacreous
Sorry I misplaced a bracket above. Try this;
if { $redirect_to_mobile eq "true" && [HTTP::status] == 404 } { - Greg_33932
Nimbostratus
Sorry, I'm sure I'm annoying now. I get the following when saving your string above from the iRule editor. it appears to not like it without the bracket.
line 30: [command is not valid in current event context (HTTP_REQUEST)] [HTTP::status]
- IheartF5_45022
Nacreous
I'm sorry - sometimes I answer without reading the whole thing, so I missed you were trying to access HTTP::status from HTTP_REQUEST. HTTP::status is a serverside attribute - no accessible from HTTP_REQUEST. I don't really understand what that whole 'if' statement is for BTW - I think you could just drop it altogether without changing the logic-flow through your iRule;-
when CLIENT_ACCEPTED { set group "group_web_uri_Mobile" set group_web_mobile_user_agents "group_mobile_user_agents" } when HTTP_REQUEST { Set the user-agent field to all lower case so that the data group matches are not case sensitive set user_agent [string tolower [HTTP::header User-Agent]] set non_mobile_pool "qa.bbb.com" set mobile_url "http://mobileqa.bbb.com" set redirect_to_mobile "false" set group $::group_uri_Mobile Determine if the user agent (all lower case) is a member of the data group if { [class match $user_agent contains $group_web_mobile_user_agents] } { log local0. "Mobile - User-agent ($user_agent) is a member of the group_mobile_user-agents data group" set redirect_to_mobile "true" Check to see if it is an Android tablet. If it is, do not send it to the mobile site if { ( $user_agent contains "android" ) && not( $user_agent contains "mobile" ) } { log local0. "Mobile - User-agent ($user_agent) contains Android but is not Mobile" set redirect_to_mobile "false" } } else { log local0. "Mobile - User-agent ($user_agent) is NOT a member of the group_mobile_user-agents data group" set redirect_to_mobile "false" } Now send the request to the appropriate destination based upon the value of the redirect_to_mobile variable if { ( $redirect_to_mobile == "true" && [class match [string tolower [HTTP::uri]] starts_with $group] ) } { HTTP::redirect "$mobile_url[HTTP::uri]" } else { pool $non_mobile_pool } }
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* 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