Forum Discussion

Andy_Bourke_497's avatar
Andy_Bourke_497
Icon for Nimbostratus rankNimbostratus
Apr 20, 2009

Multiple redirects on one VIP

Hi,

 

 

We have a virtual server that I've been configuring and I need to write an iRule for some specific functionality.

 

 

We have 10 regional sites that all point to one VIP. I want to have users hit the root of the website and be redirected to the application, however each region should be redirected to a different 'variable' in the uri.

 

 

Eg:

 

 

http://loc1.test.com should redirect to http://loc1.test.com/web/app/do.pl?login=auto&region=1

 

http://loc2.test.com should redirect to http://loc2.test.com/web/app/do.pl?login=auto&region=2

 

 

I think this should be fairly basic, but I'm certainly not a coder and the iRules that I have thought of have seemed pretty ugly and not very effecient.

 

 

Any pointers anyone can think of would be appreciated.

 

 

Thanks,

 

Andy
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Hi Andy,

    It could be a very concise iRule if you use a regex. However, a string function would be more efficient (but longer and less exact). Here are one example for each:

     
     when HTTP_REQUEST { 
      
         Log a debug line for every request 
        log local0. "[IP::client_addr]:[TCP::client_port]: New request to [HTTP::host][HTTP::uri]" 
      
         Check if there are any digits in the host header value before a colon, 
           saving the digits in the $digits variable 
        if {[regexp {[^0-9:]+([0-9]+)[^0-9]*} [HTTP::host] -> digits]}{ 
      
            There was at least one digit in the host header value 
           log local0. "[IP::client_addr]:[TCP::client_port]: Matched: $digits, redirecting." 
      
           HTTP::redirect "http://[HTTP::host]/web/app/do.pl?login=auto&region=$digits" 
      
        } else { 
      
            There were no digits in the host header value (before a colon).  Take some default action? 
           log local0. "[IP::client_addr]:[TCP::client_port]: No match" 
        } 
     } 
     

     
     when HTTP_REQUEST { 
      
         Log a debug line for every request 
        log local0. "[IP::client_addr]:[TCP::client_port]: New request to [HTTP::host][HTTP::uri]" 
      
         Redirect based on the host header value 
        switch [HTTP::host] { 
           "*1*" { 
              HTTP::redirect "http://[HTTP::host]/web/app/do.pl?login=auto&region=1" 
              log local0. "[IP::client_addr]:[TCP::client_port]: 1" 
           } 
           "*2*" { 
              HTTP::redirect "http://[HTTP::host]/web/app/do.pl?login=auto&region=2" 
              log local0. "[IP::client_addr]:[TCP::client_port]: 2" 
           } 
           "*3*" { 
              HTTP::redirect "http://[HTTP::host]/web/app/do.pl?login=auto&region=3" 
              log local0. "[IP::client_addr]:[TCP::client_port]: 3" 
           } 
           default { 
               This case will be hit if the request didn't have a digit in the  
                 host header value or didn't have a host header at all.  
              HTTP::redirect "http://hardcodedhost.example.com/web/app/do.pl?login=auto&region=default" 
              log local0. "[IP::client_addr]:[TCP::client_port]: No match, take some default action?" 
           } 
        } 
     } 
     

    Aaron
  • Apologies - I should have explained a bit more.

     

     

    I will have a try at getting the redirect loop to stop!

     

     

    Thanks again for your effort!!!
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    If all of the domains resolve to the ViP that the iRule is configured on, you probably want to limit the iRule by checking the requested path first. If the path equals "/", then check the requested host header value. You can add this check to the top of the iRule:

     
      when HTTP_REQUEST {  
      
          Log a debug line for every request  
         log local0. "[IP::client_addr]:[TCP::client_port]: New request to [HTTP::host][HTTP::uri]"  
      
          Check if requested path is / 
         if {[HTTP::path eq "/"}{ 
      
             Redirect based on the host header value  
            switch [HTTP::host] {  
               "*1*" {  
        ... 
     

    And you would need to remove the HTTP::redirect from the default case.

    Aaron
  • i actually just appended the uri to the switch and added the trailing / to the request - seems to have done the trick so far. dont know if it was the correct thing to do though!

      
     switch [HTTP::host][HTTP::uri] {   
     "location.webserver.com/" {   
     HTTP::redirect "http://[HTTP::host]/blah/blah/app/do.pl?region=R1"   
     log local0. "[IP::client_addr]:[TCP::client_port]: Region1"   
     }  
     
  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Yep, that has the same effect. Checking for HTTP::path eq / before entering the switch statement should be a bit more efficient, but your method should provide the same functionality.

     

     

    Aaron