# irule User Agent rewrite

**URL:** <https://community.f5.com/t/irule-user-agent-rewrite/38592>\
**Category:** Technical Forums\
**Tags:** irules, dev, devops, application-delivery\
**Created:** [January 6, 2009, 6:46pm UTC](https://community.f5.com/t/irule-user-agent-rewrite/38592 "2009-01-06T18:46:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javon\_Simons\_47](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@Javon\_Simons\_47](https://community.f5.com/u/Javon_Simons_47)\
**Post date:** [January 6, 2009, 6:46pm UTC](https://community.f5.com/t/irule-user-agent-rewrite/38592/1 "2009-01-06T18:46:54Z")

</div>

IE problem. It seems that the more .NET updates that you install, the longer your user agent becomes in your registry. I was wondering if this irule would rewrite, IE shorten the user agent:

when HTTP\_REQUEST {

change host header

if { [HTTP::header “User-Agent”] contains " .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30; InfoPath.2; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729 " }

{

HTTP::header replace “; .NET CLR 1.1.4322” " "

HTTP::header replace “; .NET CLR 3.0.04506.648” " "

HTTP::header replace “; .NET CLR 3.0.04506.30” " "

}

}

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [January 6, 2009, 8:17pm UTC](https://community.f5.com/t/irule-user-agent-rewrite/38592/2 "2009-01-06T20:17:19Z")

</div>

Out of curiosity, why are you trying to trim down the length of the User-Agent header? Does your app not handle the long header value? Could you just truncate it or replace it with a default value if the User-Agent header is over a certain length and contains “.net clr”?

If not, you could use a regex to replace .net clr followed by the version string.

‘HTTP::header replace’ expects the header name and the new value for the header in the following format: ‘HTTP::header replace header\_name new\_value’.

```tcl
when HTTP_REQUEST {  
    
      Rewrite the User-Agent header value if it's over 100 characters and contains .NET CLR  
     if { [string length [HTTP::header "User-Agent"]] > 100 && [string tolower [HTTP::header "User-Agent"]] contains " .net clr"}{  
    
         Replace the User-Agent header with a default value  
       HTTP::header replace "User-Agent" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.1; )"  
    
         A more precise option (but also more resource intensive)  
         Replace the .NET CLR strings in the User-Agent header with nothing, using a regex  
        HTTP::header replace "User-Agent" [regsub -all -nocase {\.net clr (?:\d{1,5}\.?){1,4};? ?} [HTTP::header "User-Agent"] ""]  
     }  
  }

```

Here is a description of the regex:

\.net clr (?:\d{1,5}\.?){1,4};? ?

Match the character “.” literally «\.»

Match the characters "net clr " literally «net clr »

Match the regular expression below «(?:\d{1,5}\.?){1,4}»

Between one and 4 times, as many times as possible, giving back as needed (greedy) «{1,4}»

Match a single digit 0..9 «\d{1,5}»

Between one and 5 times, as many times as possible, giving back as needed (greedy) «{1,5}»

Match the character “.” literally «\.?»

Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

Match the character “;” literally «;?»

Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

Match the character " " literally « ?»

Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

Aaron

---

<div class="post-metadata">

**Author:** ![Javon\_Simons\_47](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@Javon\_Simons\_47](https://community.f5.com/u/Javon_Simons_47)\
**Post date:** [January 7, 2009, 1:56pm UTC](https://community.f5.com/t/irule-user-agent-rewrite/38592/3 "2009-01-07T13:56:52Z")

</div>

Thank you for replying. Yes, the app is having trouble with the long string. It causes DOS of the website in IE. Your first option looks like the way I should go. Thank you.
