# stripping data

**URL:** <https://community.f5.com/t/stripping-data/28037>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [May 28, 2008, 6:17pm UTC](https://community.f5.com/t/stripping-data/28037 "2008-05-28T18:17:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![slackwaresuppor](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@slackwaresuppor](https://community.f5.com/u/slackwaresuppor)\
**Post date:** [May 28, 2008, 6:17pm UTC](https://community.f5.com/t/stripping-data/28037/1 "2008-05-28T18:17:49Z")

</div>

how would i strip the www. off of [www.domain.com](http://www.domain.com)??

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [May 28, 2008, 6:24pm UTC](https://community.f5.com/t/stripping-data/28037/2 "2008-05-28T18:24:44Z")

</div>

Like this:

```tcl
[getfield [HTTP::host] ".domain.com" 2]

```

---

<div class="post-metadata">

**Author:** ![slackwaresuppor](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@slackwaresuppor](https://community.f5.com/u/slackwaresuppor)\
**Post date:** [May 28, 2008, 6:29pm UTC](https://community.f5.com/t/stripping-data/28037/3 "2008-05-28T18:29:30Z")

</div>

when HTTP\_REQUEST {

HTTP::redirect [https://www.domain1.com/nsa/+homePage=\](https://www.domain1.com/nsa/+homePage=%5C)[getfield [HTTP::host] “.domain.com” 2]

}

that correct?? bc it does not work… i just get

[https://www.domain1.com/nsa/+homePage=](https://www.domain1.com/nsa/+homePage=)

---

<div class="post-metadata">

**Author:** ![slackwaresuppor](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@slackwaresuppor](https://community.f5.com/u/slackwaresuppor)\
**Post date:** [May 28, 2008, 6:49pm UTC](https://community.f5.com/t/stripping-data/28037/4 "2008-05-28T18:49:43Z")

</div>

also would like to try and put in an if statement, if it has www, strip it off, if not, just forward with the domainname.

---

<div class="post-metadata">

**Author:** ![slackwaresuppor](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@slackwaresuppor](https://community.f5.com/u/slackwaresuppor)\
**Post date:** [May 28, 2008, 7:00pm UTC](https://community.f5.com/t/stripping-data/28037/5 "2008-05-28T19:00:38Z")

</div>

so this is what ive come up with..

when HTTP\_REQUEST {

if {[getfield [HTTP::host] “.domain.com” 1] eq “www” }

{HTTP::redirect [https://www.domain1.com/nsa/+homePage=\](https://www.domain1.com/nsa/+homePage=%5C)[getfield [HTTP::host] “.domain.com” 2]

}

else {HTTP::redirect [https://www.domain1.com/nsa/+homePage=\[HTTP::host]](https://www.domain1.com/nsa/+homePage=%5C%5BHTTP::host%5D)}

}

the non www is working.. the www is not.. any ideas?

---

<div class="post-metadata">

**Author:** ![slackwaresuppor](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@slackwaresuppor](https://community.f5.com/u/slackwaresuppor)\
**Post date:** [May 28, 2008, 7:03pm UTC](https://community.f5.com/t/stripping-data/28037/6 "2008-05-28T19:03:58Z")

</div>

also.. the [domain.com](http://domain.com) could be anything not just a single domain, so i need to check all incoming www.?

not just [www.domain.com](http://www.domain.com)

---

<div class="post-metadata">

**Author:** ![slackwaresuppor](https://avatars.discourse-cdn.com/v4/letter/s/a6a055/32.png) [@slackwaresuppor](https://community.f5.com/u/slackwaresuppor)\
**Post date:** [May 28, 2008, 8:23pm UTC](https://community.f5.com/t/stripping-data/28037/7 "2008-05-28T20:23:54Z")

</div>

ahhhhh…

SOLUTION:

when HTTP\_REQUEST {

if {[getfield [HTTP::host] “.” 1] starts\_with “www” }

{HTTP::redirect [https://www.domain.com/nsa/+homePage=\](https://www.domain.com/nsa/+homePage=%5C)[getfield [HTTP::host] “.” 2].[getfield [HTTP::host] “.” 3]}

else {HTTP::redirect [https://www.domain.com/nsa/+homePage=\[HTTP::host]](https://www.domain.com/nsa/+homePage=%5C%5BHTTP::host%5D)}

}

---

<div class="post-metadata">

**Author:** ![Joe\_Pruitt](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/joe_pruitt/32/8438_2.png) [@Joe\_Pruitt](https://community.f5.com/u/Joe_Pruitt)\
**Post date:** [May 28, 2008, 8:27pm UTC](https://community.f5.com/t/stripping-data/28037/8 "2008-05-28T20:27:26Z")

</div>

In this example you are doing the getfield twice which is causing double string allocations. If all you want to do is check whether a “www.” is prefixing your domain, you can do so with the “starts\_with” operator. And then, since you know you want to strip off the first 4 characters, you can use the “string range” command to do that.

This code will set a variable to the value of the requested HTTP::host. If that value starts with a “www.”, it will strip the “www.” off and assign that to the variable. It will then issue a redirect with either the original or stripped host value.

```tcl
when HTTP_REQUEST { 
   set host [HTTP::host] 
   if { [HTTP::host] starts_with "www." } { 
     set host [string range [HTTP::host] 4 end] 
   } 
   HTTP::redirect "https://www.domain1.com/nsa/+homePage=$host" 
 }

```

Is this what you are going for?

Oh, BTW, make sure you don’t have this iRule on your [www.domain1.com](http://www.domain1.com) virtual or you’ll get yourself into an infinite loop.

-Joe

---

<div class="post-metadata">

**Author:** ![John\_Alam\_45640](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@John\_Alam\_45640](https://community.f5.com/u/John_Alam_45640)\
**Post date:** [May 29, 2008, 2:30pm UTC](https://community.f5.com/t/stripping-data/28037/9 "2008-05-29T14:30:55Z")

</div>

Since it is fun we are after, how is this:

when HTTP\_REQUEST {

HTTP::redirect “[https://www.domain1.com/nsa/+homePage=](https://www.domain1.com/nsa/+homePage=)”+[regsub {^www\.} [HTTP::host] “”]

}

If i got all the synthax correct, regsub should remove www. if it finds it, if it does not, it should return [HTTP::host} as is.

John

---

<div class="post-metadata">

**Author:** ![Deb\_Allen\_18](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Deb\_Allen\_18](https://community.f5.com/u/Deb_Allen_18)\
**Post date:** [May 29, 2008, 4:27pm UTC](https://community.f5.com/t/stripping-data/28037/10 "2008-05-29T16:27:06Z")

</div>

Ouch, please let’s _not_ go with regsub to solve such a simple problem. Very pretty all on one line & everything, but very expensive command to invoke regardless how simple the operation itself might be.

Why not simply use the “domain” command:

```tcl
when HTTP_REQUEST {  
      HTTP::redirect "https://www.domain1.com/nsa/+homePage=[domain [HTTP::host] 2]"      
  }

```

which will return only the last 2 parts of any domain name.

If you truly need to operate only on those domains with www. prefix, then use the starts\_with operator to condition the change as in colin’s example:

```tcl
when HTTP_REQUEST {  
     if { [HTTP::host] starts_with "www." } {        
        HTTP::redirect "https://www.domain1.com/nsa/+homePage=[domain [HTTP::host] 2]"      
     } else {       
         HTTP::redirect "https://www.domain1.com/nsa/+homePage=[HTTP::host]"        
     }       
  }

```

/deb

---

<div class="post-metadata">

**Author:** ![Deb\_Allen\_18](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@Deb\_Allen\_18](https://community.f5.com/u/Deb_Allen_18)\
**Post date:** [May 29, 2008, 4:29pm UTC](https://community.f5.com/t/stripping-data/28037/11 "2008-05-29T16:29:49Z")

</div>

oh, that was ugly.

re-submitted, hopefully you can read it now…
