Forum Discussion

Darrell_Walker_'s avatar
Darrell_Walker_
Icon for Nimbostratus rankNimbostratus
Jan 04, 2006

Redirect based on port number

I'm looking for an irule that can do redirects based on certain port numbers.

 

 

For example:

 

 

http://server.domain.com:8888

 

 

to be redirected to:

 

 

https://server2.domain.com:8888

 

 

every other port to:

 

 

https://server3.domain.com

 

 

The actual code I'm toying with looks like:

 

 

when HTTP_REQUEST {

 

if {[IP::client_port] = 8888} {

 

HTTP::redirect https://[HTTP::host][HTTP::uri]:8888

 

}

 

else {

 

HTTP::redirect https://[HTTP::host][HTTP::uri]

 

}

 

}
  • I see several problems with your code

     

     

    1) IP::client_port is not a valid command. Try using TCP::local_port instead.

     

    2) Your first redirect has the port after the URI, it should be after the host and before the URI.

     

    3) Your code has many situations for infinite loops.

     

     

    It's not clear from your first request whether you want to redirect to different domains (as you stated in your description) or the same domain (as you did in your iRule).

     

     

    If I go with the assumption that you want to do what you stated in words, something like this should work

     

     

    when HTTP::REQUEST {
      log local0. "routing host [HTTP::host] and port [TCP::server_port]"
      if { ([TCP::local_port] == 8888) && ([HTTP::host] != "server2.domain.com") } {
        log local0. "Redirecting to https://server2.domain.com:8888[HTTP::uri]"
        HTTP::redirect https://server2.domain.com:8888[HTTP::uri]
      } elseif { ([TCP::local_port] != 443) || ([HTTP::host] != "server3.domain.com") } {
        log local0. "Redirecting to https://server3.domain.com[HTTP::uri]"
        HTTP::redirect https://server3.domain.com[HTTP::uri]
      }
    }

     

     

    I haven't fully thought out all combinations of your logic, but this should at least get you on the road to working things out.

     

     

    Use the log statements to trace tests of your request flow (look in the /var/log/ltm file on the BIG-IP) and make sure all your options are covered.

     

     

    Good luck and let us know what you end up with.

     

     

    -Joe