iRule Help/HTTP:respond assistance

Hello,

I was wondering if someone could give me a hand with an iRule. I am trying to redirect all test.website.com traffic to test.website2.com EXCEPT traffic going to test.website.com/Three (I don’t want traffic going to test.website2.com/Three). Can anyone tell me what I am doing wrong? I have attached my iRule as a reference.

when HTTP_REQUEST {

if { [HTTP::host] equals “test.website.com”} {

HTTP::respond 301 Location http://test.website2.com[HTTP::uri]

}

elseif { [HTTP::host] equals “test.website.com” and [HTTP::uri] equals “/One/Two/” } {

HTTP::respond 301 Location http://test.website2.com/One/Two

}

elseif { [HTTP::host] equals “test.website.com” and [HTTP::uri] equals “/Three/” } {

HTTP::respond 301 Location http://test.website.com/Three

}

Thanks in advance

Hi Ben,

I think one issue with your iRule is that you’d redirect any request with a host header value of test.website.com to test.website2.com[HTTP::uri] before checking the URI.

How about something like this? If you want an exact match only on the URI, remove the -glob flag from the switch command and the * at the end of /Three/.

when HTTP_REQUEST { 
  
    log local0. "[IP::client_addr]:[TCP::client_port]: New [HTTP::method] request to [HTTP::host][HTTP::uri]" 
  
     Check requested host header value 
    if {[string tolower [HTTP::host]] eq "test.website.com"} { 
  
       log local0. "[IP::client_addr]:[TCP::client_port]: Matched Host check.  Checking URI now." 
  
        Check the requested URI 
       switch -glob [HTTP::uri] { 
          "/Three*" { 
               Requested URI started with /Three 
              log local0. "[IP::client_addr]:[TCP::client_port]: Matched URI check for /Three. Allowing request to go to pool" 
          } 
          default { 
             HTTP::respond 301 Location "http://test.website2.com[HTTP::uri]" 
              log local0. "[IP::client_addr]:[TCP::client_port]: No URI match. Redirecting to http://test.website2.com[HTTP::uri]" 
          } 
       } 
    } 
 }

Aaron

Thanks you sir! I will try it out and see if it works. I will post back my results.

It worked and I got sign-off, so thanks for that Hoolio. In this section here:

switch -glob [HTTP::uri] {

“/Three*” {

Requested URI started with /Three

log local0. “[IP::client_addr]:[TCP::client_port]: Matched URI check for /Three. Allowing request to go to pool”

}

Can you add other URIs? (e.g. “/Three*” or “/Four*”)?

I think I found the answer:

I just write something like this:

“Three*” -

“Four*” -

“Five*” {

And that seems to do it.

Thanks again.

Sure, for switch cases that you want to take the same action for, you can add them using -

For details, you can check the TCL switch page: http://www.tcl.tk/man/tcl8.4/TclCmd/switch.htm

Here are a few example of using glob style patterns:

switch -glob $some_string { 
    "1" -  
    "2" -  
    "3" { 
        Matched 1, 2 or 3 
    } 
    "[4-6]" { 
        Matched 4, 5 or 6 
    } 
    "[d-f]" { 
        Matched d, e or f 
    } 
    "z*" { 
        Matched anything starting with z 
    } 
    "*z" { 
        Matched anything ending with z 
    } 
    default { 
        No match. Default case 
    } 
 }
when HTTP_REQUEST {  
    
     log local0. "[IP::client_addr]:[TCP::client_port]: New [HTTP::method] request to [HTTP::host][HTTP::uri]"  
    
      Check requested host header value  
     if {[string tolower [HTTP::host]] eq "test.website.com"} {  
    
        log local0. "[IP::client_addr]:[TCP::client_port]: Matched Host check.  Checking URI now."  
    
         Check the requested URI  
        switch -glob [HTTP::uri] {  
           "/Three*" -  
           "/Four*" { 
                Requested URI started with /Three or /Four 
               log local0. "[IP::client_addr]:[TCP::client_port]: Matched URI check for /Three or /Four. Allowing request to go to pool"  
           }  
           default {  
              HTTP::respond 301 Location "http://test.website2.com[HTTP::uri]"  
               log local0. "[IP::client_addr]:[TCP::client_port]: No URI match. Redirecting to http://test.website2.com[HTTP::uri]"  
           }  
        }  
     }  
  }

Aaron

Thanks again. This was a huge help.