Forum Discussion

jba3126's avatar
jba3126
Icon for Cirrus rankCirrus
Mar 31, 2017

iRule to Rewrite User Agent Header

We have a client that has an internal browser policy for IE to run in compatibility mode due to legacy applications. Unfortunately with the latest update to our application servers this causes issues with the content displaying properly. The application displays fine when the User Agent appears as one that is compatible. I have an iRule that I believe will work in concept; however I'm getting errors (See Below).

when HTTP_REQUEST { Rewrite the User-Agent header value to show up supported browser if { [string toupper [HTTP::header User-Agent]] contains “MSIE 6” or “MSIE 7” or “MSIE 8” or “MSIE 9” or “MSIE 10”)}{ Replace the User-Agent header with supported user agent

HTTP::header replace “User-Agent” "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"

} }

01070151:3: Rule [/Common/HTTP-UserAgentHeader-Rewrite-iRule] error: /Common/HTTP-UserAgentHeader-Rewrite-iRule:3: error: [parse error: PARSE syntax 156 {syntax error in expression " [string toupper [HTTP::header User-Agent]] contains “...": unexpected operator &}][{ [string toupper [HTTP::header User-Agent]] contains “MSIE 6” or “MSIE 7” or “MSIE 8” or “MSIE 9” or “MSIE 10”)}] /Common/HTTP-UserAgentHeader-Rewrite-iRule:5: error: [undefined procedure: User-Agent&8221][User-Agent”] /Common/HTTP-UserAgentHeader-Rewrite-iRule:5: error: [undefined procedure: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko]["Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"]

5 Replies

  • I've updated the rule and was able to figure out the syntax errors; however I'm getting the following the logs.

     

    when HTTP_REQUEST {

     

    Rewrite the User-Agent header value to show up supported browser

    log local0. "[HTTP::header User-Agent]" if { [string toupper [HTTP::header "User-Agent"]] contains "MSIE 6" or "MSIE 7" or "MSIE 8" or "MSIE 9" or "MSIE 10"}{

     

    Replace the User-Agent header with supported user agent

    HTTP::header replace "User-Agent" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" log local0. "[HTTP::header User-Agent]" } }

     

    Mar 31 18:01:20 f5-ve info tmm1[20879]: Rule /Common/HTTP-UserAgentHeader-Rewrite-iRule : Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0E; .NET4.0C) Mar 31 18:01:20 f5-ve err tmm1[20879]: 01220001:3: TCL error: /Common/HTTP-UserAgentHeader-Rewrite-iRule - can't use non-numeric string as operand of "||" while executing "if { [string toupper [HTTP::header "User-Agent"]] contains "MSIE 6" or "MSIE 7" or "MSIE 8" or "MSIE 9" or "MSIE 10"}{ Replace the User-Agent header..."

     

  • You have another approach, using the switch statement:

    when HTTP_REQUEST { 
         Rewrite the User-Agent header value to show up supported browser 
        switch -glob [string toupper [HTTP::header User-Agent]] {
            "*MSIE 6*" -
            "*MSIE 7*" -
            "*MSIE 8*" -
            "*MSIE 9*" -
            "*MSIE 10*" {
                 Replace the User-Agent header with supported user agent
                HTTP::header replace "User-Agent" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" 
            }
            default {
            }
        }
    }
    

    A better approach is not using iRule but using a traffic policy (Local Traffic ›› Policies)

  • Hi,

    you can merge MSIE 6 to 9 versions in one switch value:

    when HTTP_REQUEST { 
         Rewrite the User-Agent header value to show up supported browser 
        switch -glob -- [string toupper [HTTP::header User-Agent]] {
            "*MSIE [6789]*" -
            "*MSIE 10*" {
                 Replace the User-Agent header with supported user agent
                HTTP::header replace "User-Agent" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" 
            }
            default {
            }
        }
    }
    

    Also use -- between the last switch parameter and the string to compare. it will prevent minus character in string to be evaluated as another parameter.

  • try this code which does the same as your irule (optimized):

     

    when HTTP_REQUEST { 
         Rewrite the User-Agent header value to show up supported browser 
        switch -glob -- [string toupper [HTTP::header User-Agent]] {
            "*MSIE [6789]*" {
                HTTP::header replace "User-Agent" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" 
                log local0. "[HTTP::header User-Agent]" 
            }
            default {
            }
        }
    }
  • If you would like to use a traffic policy which I think would be a better approach:

     

    1. From "Local Traffic ›› Policies ›› Policy List" Create a new policy
    2. In that policy create a new rule with the following condition and action:

       

      • HTTP Header named User-Agent contains any of XXX at request time

         

      • Replace HTTP Header named User-Agent with value YYY at request time

         

      For XXX, add each of your specific strings (MSIE 7, ...), I'm not sure if this field accept regular expressions but you can test the regex suggested by Stanisla.

       

      Replace YYY with your replacement string.

       

    3. Save and publish your policy, then associate it with your VS.