Forum Discussion

dp_119903's avatar
dp_119903
Icon for Cirrostratus rankCirrostratus
Mar 27, 2015

iRule with User Agent to allow iphone and android, but block everything else

I have read a bunch of articles/questions/answers regarding irules and how to block user agents. I am relatively comfortable blocking a browser or taking an action based on looking at the user agent, but I'm having trouble with syntax.

I have an application that we want to allow access to when using an iphone or android (via the app), but we want to block access when users try to access the site via a browser. After logging the attempts when accessing the app via the ios device and android device I can see that in the user agent string the ios device inserted "iphone" and the android device inserted "android". As a simple test it seems like if I wrote an irule that says "if the user agent has iphone or android in the user-agent then do the following, but if it doesn't then redirect it to another site.

However, the redirect works, but it ends up breaking the iphone app.

Here is the irule I had in place before trying to block the browsers (cause the app owner said I needed to have this redirect in place)

when HTTP_REQUEST {
    Check if path is /
   if {[HTTP::path] eq "/"}{

       Send 302 redirect to the new location
      HTTP::redirect https://[HTTP::host]/PATH/GOES/HERE   }
}

Now when I add the following it redirects me to the test site when I access the site from a browser on my windows machine, but in turn it breaks the iphone app.

when HTTP_REQUEST {
  if { [HTTP::header User-Agent] != "iphone"} {
  HTTP::redirect http://www.testsitegoeshere.com   

} if {[HTTP::path] eq "/"}{

   Send 302 redirect to the new location
  HTTP::redirect https://[HTTP::host]/PATH/GOES/HERE   }

}

In the end all I'm trying to do is block anything that is not identifying itself as an ios or android device (by block I mean redirect them elsewhere)...and if they are an ios or android device then I simply want to let them in redirect them if their path is "/" to the "PATH/GOES/HERE" URI that works when I take off the iphone text.

And oh yeah, I cannot figure out how to make a statement that says "if the user agent contains iphone or android" or "if the user agent does not contain iphone or android". Every attempt I made to do this resulted in some form of syntax error. Any guidance would be appreciated.

1 Reply

  • giltjr's avatar
    giltjr
    Icon for Nimbostratus rankNimbostratus

    Try:

    if { ([HTTP::header User-Agent] contains "iphone") or ([HTTP::header User-Agent] contains "Android") }