Forum Discussion

Joe_Pipitone's avatar
Joe_Pipitone
Icon for Nimbostratus rankNimbostratus
Jun 05, 2009

Redirect - adding elseif

I want to add multiple redirect statements to a single iRule, when a match is found, redirect, else, keep going until i find a match. Can anyone tell me what i am missing here?

 

 

when HTTP_REQUEST {

 

if { ([HTTP::host] eq "domain1.com") } {

 

HTTP::redirect "http://domain2.com"

 

} elseif { (HTTP::host] eq "domain3.org") } {

 

HTTP::redirect "http://domain4.org"

 

}

 

}

 

 

This is the error the big-ip is giving me:

 

 

01070151:3: Rule [Redirect_test] error:

 

line 4: [parse error: PARSE syntax 132 {syntax error in expression " (HTTP::host] eq "domain3.org") ": variable references require preceding $}] [{ (HTTP::host] eq "domain3.org") }]
  • OK, so I did some further reading and found it was best to use a switch statement.

     

     

    The problem now is, I would like to listen for www.domain1.com as well as domain1.com, with and without the www. I tried using && and ||, each triggered a syntax error. This is my iRule:

     

     

    when HTTP_REQUEST {

     

    set h [HTTP::host]

     

    set u [HTTP::uri]

     

    switch -glob $h {

     

    "domain1.com" {

     

    HTTP::redirect "http://domain2.com"

     

    }

     

    "domain3.org" {

     

    HTTP::redirect "http://domain4.org"

     

    }

     

     

    }

     

    }
  • Good approach with the switch! If you were wondering, the second "HTTP::host" was missing the beginning left bracket before it.

     

     

    -Joe
  • Do you know how I can also listen for www.domain1.com as well as domain1.com - both with and without the www? I tried using && and ||, but it was throwing an error with my switch iRule. I have multiple host headers that I would like to redirect.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    For both you'd just use the glob matching, like you already are, and add an asterisk, like so:

     
     when HTTP_REQUEST { 
       set h [HTTP::host] 
       set u [HTTP::uri] 
       switch -glob $h { 
         "*domain1.com" { 
           HTTP::redirect "http://domain2.com" 
         } 
         "*domain3.org" { 
           HTTP::redirect "http://domain4.org" 
         } 
       } 
     } 
     

    This would, of course, match anything before the domain, including all cnames. If you wanted to be more specific you could just add more match cases:

     
     when HTTP_REQUEST { 
       set h [HTTP::host] 
       set u [HTTP::uri] 
       switch -glob $h { 
         "www.domain1.com" - 
         "domain1.com" { 
           HTTP::redirect "http://domain2.com" 
         } 
         "www.domain3.org" - 
         "domain3.org" { 
           HTTP::redirect "http://domain4.org" 
         } 
       } 
     } 
     

    hth,

    Colin