Forum Discussion

10 Replies

  • when HTTP_REQUEST {

     

    HTTP::redirect https://www.domain1.com/nsa/+homePage=[getfield [HTTP::host] ".domain.com" 2]

     

    }

     

     

     

     

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

     

    https://www.domain1.com/nsa/+homePage=
  • 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.
  • 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=[getfield [HTTP::host] ".domain.com" 2]

     

    }

     

    else {HTTP::redirect https://www.domain1.com/nsa/+homePage=[HTTP::host]}

     

    }

     

     

     

    the non www is working.. the www is not.. any ideas?
  • also.. the domain.com could be anything not just a single domain, so i need to check all incoming www.?

     

    not just www.domain.com
  • ahhhhh...

     

     

     

    SOLUTION:

     

     

    when HTTP_REQUEST {

     

    if {[getfield [HTTP::host] "." 1] starts_with "www" }

     

    {HTTP::redirect https://www.domain.com/nsa/+homePage=[getfield [HTTP::host] "." 2].[getfield [HTTP::host] "." 3]}

     

    else {HTTP::redirect https://www.domain.com/nsa/+homePage=[HTTP::host]}

     

    }
  • 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.

     

     

    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 virtual or you'll get yourself into an infinite loop.

     

     

    -Joe
  • John_Alam_45640's avatar
    John_Alam_45640
    Historic F5 Account
    Since it is fun we are after, how is this:

     

     

    when HTTP_REQUEST {

     

    HTTP::redirect "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
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    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:
     
     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:
      
      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
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    oh, that was ugly.

     

    re-submitted, hopefully you can read it now...