Forum Discussion

mfkk531_168091's avatar
mfkk531_168091
Icon for Nimbostratus rankNimbostratus
Apr 17, 2015
Solved

Question on Redirect to mobile website - plz help

I have been given an old iRule to implement now to redirect to mobile site. But i suspect there is some syntax errors. Can someone help me check the rule and see if its good, and will work for my requirement.

when HTTP_REQUEST {
  if { [matchclass [HTTP::header User-Agent] contains $::abcd_useragents] } {
    if { [URI::query [HTTP::uri] "MobileOptOut"] ne 1 && [HTTP::cookie MobileOptOut] ne 1} {
      if { not ([HTTP::uri] ends_with "jpg" ||
                [HTTP::uri] ends_with "gif" ||
                [HTTP::uri] ends_with "png" ||
                [HTTP::uri] ends_with "bmp" ||
                [HTTP::uri] ends_with "ico") } {
        HTTP::redirect "http://m.xyzdomain.com[HTTP::uri]"
      }
    }
  }
}

Thanks

  • Had an extra ] at the end of the third if statement. This should fix it for you.

    when HTTP_REQUEST {
      if {[class match [HTTP::header User-Agent] contains abcd_useragents] } {
        if {[HTTP::cookie MobileOptOut] eq 1} { return }
        if {[URI::query [HTTP::uri] MobileOptOut] eq 1} { return }
        switch -glob [string tolower [HTTP::path]] {
          "*.jpg" -
          "*.gif" -
          "*.png" -
          "*.bmp" -
          "*.ico" { return }
        }
        HTTP::redirect "http://m.xyzdomain.com[HTTP::uri]"
      }
    }
    

8 Replies

  • If I was to refresh it would look like the below. The problem with the filetype match against HTTP::uri is if your URI is /page.jpg?value=1 it will fail to match, you need to match against HTTP::path which is everything up to the "?" in the URI. The return command below just mean to exit this irule.

    when HTTP_REQUEST {
      if {[class match [HTTP::header User-Agent] contains abcd_useragents] }
        if {[HTTP::cookie MobileOptOut] eq 1} { return }
        if {[URI::query [HTTP::uri] MobileOptOut] eq 1]} { return }
        switch -glob [HTTP::path] {
          "*.jpg" -
          "*.gif" -
          "*.png" -
          "*.bmp" -
          "*.ico" { return }
        }
        HTTP::redirect "http://m.xyzdomain.com[HTTP::uri]"
      }
    }
    
  • I get the following error

    01070151:3: Rule [/Common/iRule_BrandingBrand_Redirect_IVD] error: /Common/iRule_BrandingBrand_Redirect_IVD:2: error: [missing a script after "if"][]
    /Common/iRule_BrandingBrand_Redirect_IVD:4: error: [parse error: PARSE syntax 205 {syntax error in expression "[URI::query [HTTP::uri] MobileOptOut] eq 1]": extra tokens at end of expression}][{[URI::query [HTTP::uri] MobileOptOut] eq 1]}]
    /Common/iRule_BrandingBrand_Redirect_IVD:14: error: [command is not valid in the current scope][}]
    
    • Kevin_Davies_40's avatar
      Kevin_Davies_40
      Icon for Nacreous rankNacreous
      Was missing a opening brace at the end of the first if statement. You will need to test it though to make sure it meets your needs. You may want to consider lowercasing the HTTP::path to ensure it matches the strings.
  • when HTTP_REQUEST {
      if {[class match [HTTP::header User-Agent] contains abcd_useragents] } {
        if {[HTTP::cookie MobileOptOut] eq 1} { return }
        if {[URI::query [HTTP::uri] MobileOptOut] eq 1]} { return }
        switch -glob [string tolower [HTTP::path]] {
          "*.jpg" -
          "*.gif" -
          "*.png" -
          "*.bmp" -
          "*.ico" { return }
        }
        HTTP::redirect "http://m.xyzdomain.com[HTTP::uri]"
      }
    }
    
  • Thanks so much for the help! One last thing - pls help me with this error i know im very close- all because of you

    01070151:3: Rule [/Common/test] error: /Common/test:4: error: [parse error: PARSE syntax 198 {syntax error in expression "[URI::query [HTTP::uri] MobileOptOut] eq 1]": extra tokens at end of expression}][{[URI::query [HTTP::uri] MobileOptOut] eq 1]}]

  • Had an extra ] at the end of the third if statement. This should fix it for you.

    when HTTP_REQUEST {
      if {[class match [HTTP::header User-Agent] contains abcd_useragents] } {
        if {[HTTP::cookie MobileOptOut] eq 1} { return }
        if {[URI::query [HTTP::uri] MobileOptOut] eq 1} { return }
        switch -glob [string tolower [HTTP::path]] {
          "*.jpg" -
          "*.gif" -
          "*.png" -
          "*.bmp" -
          "*.ico" { return }
        }
        HTTP::redirect "http://m.xyzdomain.com[HTTP::uri]"
      }
    }
    
  • Hi Kevin, Many many many thanks

     

    It worked like a champ!!!!

     

    Thanks again....

     

  • Hi Kevin/Anyone who can help, We have made some changes and now we dont use redirect website anywhere else like m.xyz.com, but just have it on different servers in a pool and have f5 redirect to that pool if the useragent is mobile.

    when HTTP_REQUEST {
            if { [class match [string tolower [HTTP::header User-Agent]] contains "mobile_useragents1" ] } {
                             if {[HTTP::cookie MobileOptOut] eq 1} { return }
                             if {[URI::query [HTTP::uri] MobileOptOut] eq 1} { return }         
                             pool p-mobile-redirect 
            }
            }
    

    I'm currently facing issue where the MobileOptOut feature is not working.

    I'm suspecting issue with cookies.

    I have cookie persistence 1hr activated on the vip. so anything sent to mobile pool has a cookie inserted with mobile pool memeber.

    When user hits OptOut, uri becomes ?MobileOptOut=1 he is sent to regular (desktop) pool and a new cookie with desktop pool member is inserted.

    I want tio understand how will the cookie conflict rule? I mean who will supercede whom? Will the new desktop pool cookie work over the mobile pool cookie? or vice versa?

    Thanks in advance!