Forum Discussion

Kamila_124825's avatar
Kamila_124825
Icon for Nimbostratus rankNimbostratus
May 29, 2013

Redirect to host port

Hi,

 

I want to create iRule with redirection. Something like this -> Pool under dns name mysite.com:80 should route members HOST:8080/test/

 

is the iRule like this will work?? ->

 

when HTTP_REQUEST {

 

set host [HTTP:host]

 

set uri [HTTP:uri]

 

set port 8080

 

HTTP::redirect "http://$host:$port$uri/test"

 

}

 

 

6 Replies

  • If you physically redirect the user to port 8080 you'll need a VIP that's listening on port 8080. If that's NOT what you want (you have a port 80 VIP and pool members listening on port 8080), and you just want to change the Host header to reflect this (internal) port and also change the URI, then something like this might work:

    
    when HTTP_REQUEST {
       if { not ( [string tolower [HTTP::uri]] starts_with "/test" ) } {
          HTTP::redirect "http://[HTTP::host]/test[HTTP::uri]"
       } else {
          HTTP::header replace Host "[HTTP::host]:8080"
       }
    }
    

    The above physically redirects the user (via 302) to "/test" if the original URI doesn't start with the "/test". It also adds the requested URI to the end of the path (ex. /myrequest = /test/myrequest). Not sure if you needed that part. The Host header is then replaced with the same Host header plus the ":8080" port (ex. mysite.com = mysite.com:8080). I'm assuming that since port 80 is the default for HTTP, the user request won't actually contain ":80".
  • but when I write mysite.com then will not redirect me to 8080 port.

    only to http://host/test

    I need to add :8080?

    when HTTP_REQUEST {
       if { not ( [string tolower [HTTP::uri]] starts_with "/test" ) } {
          HTTP::redirect "http://[HTTP::host]:8080/test[HTTP::uri]"
       } else {
          HTTP::header replace Host "[HTTP::host]:8080"
       }
    }
  • This all depends on your configuration. Do you want to physically redirect users to a VIP that is listening on port 8080? Or do you have a single port 80 VIP?
  • Hmm.. VIP is listening on port 80. but I think I don't need a physically redirection, because a member register in pool with 8080 port. So maybe will work only this:

    when HTTP_REQUEST {
       if { not ( [string tolower [HTTP::uri]] starts_with "/test" ) } {
          HTTP::redirect "http://[HTTP::host]/test[HTTP::uri]"
       } 
    } 

    What do you think?
  • Again, completely depends on your configuration. The HTTP::redirect command issues a physical redirect (via 302 message) to the client. This is what you're doing in your last iRule if the URI doesn't start with "/test". As for the port 8080 business, your pool should define the back end servers by IP and this port. Port translation on the VIP will take care of sending traffic to port 8080. If, however (and this is rare but it happens), the server expects to see this port in the request Host header (ex. Host = www.example.com:8080), then you need to modify the Host header on the way in.