Forum Discussion

peter_56193's avatar
peter_56193
Icon for Nimbostratus rankNimbostratus
Jul 26, 2012

irule to add string for mobile device

HI all, can some one have a look of my irule, try to add string if the request is from mobile device.

 

have setup data group MobileAgents contain mobile device key words.

 

 

when HTTP_REQUEST {

 

if { !([HTTP::uri] contains "requestAgent") }

 

{

 

if { [matchclass [HTTP::header "User-Agent"] contains $::MobileAgents] }

 

{ HTTP::redirect https://[HTTP::host]/[HTTP::uri]?requestAgent=mobile}

 

}

 

}

 

 

it looping, keep adding string at the end, how can i stop it.

 

 

thanks
  • It sounds like your request matches the code in the iRule, it is then being redirected back out, the request then comes in again, triggers the iRule and like you said just continues to loop. You need to introduce some logic there that breaks that, either by sending it to a pool of web servers or redirecting it somewhere will it will not come back through that same iRule.

     

     

    There is an excellent post here that you can probably apply:

     

    https://devcentral.f5.com/Community/GroupDetails/tabid/1082223/asg/50/aft/1178780/showtab/groupforums/Default.aspx

     

     

  • naladar, he has the right idea, that's the first if statement - !([HTTP::uri] contains "requestAgent") - note the !.

    peter, your irule worked for me, though I had to make a change to a bit of it.

    
    when HTTP_REQUEST {
      if { !([HTTP::uri] contains "requestAgent") } { 
        if { !([matchclass [HTTP::header "User-Agent"] contains $::MobileAgents]) } {
          HTTP::redirect "https://[HTTP::host][HTTP::uri]?requestAgent=mobile"
        } 
      } 
    }
    

    I put quotes around the value after HTTP::redirect, and removed the / between [HTTP::host] and [HTTP::uri], as the URI will always at a minimum contain a /. In general this probably isn't very robust - imagine a user coming in on a mobile device with the url http://example.com/?foo=bar. Your irule will redirect them to http://example.com/?foo=bar?requestAgent=mobile, which will I suppose work for the irule but will confuse your application on the backend.
  • OK, that last bit of irule, i added a ! for some other testing. Editing is apparently out of the question here, so here is the irule as I reformatted it. Ignore what I posted above.

    
    when HTTP_REQUEST {
      if { !([HTTP::uri] contains "requestAgent") } { 
        if { ([matchclass [HTTP::header "User-Agent"] contains $::MobileAgents]) } {
          HTTP::redirect "https://[HTTP::host][HTTP::uri]?requestAgent=mobile"
        } 
      } 
    }
    

    mods: can we speed this thing up a little? :[