Forum Discussion

David_L_'s avatar
David_L_
Icon for Nimbostratus rankNimbostratus
Oct 05, 2007

HTTP Redirect based on protocol

It seems like this should be simple...

 

I want to use a single rule on multiple virtual servers to redirect traffic to another site. Simplified -

 

If the incoming request is HTTP://host.com/??? redirect to HTTP://host.net/???

 

If the incoming request is HTTPS://host.com/??? redirect to HTTPS://host.net/???

 

 

What I wrote was:

 

 

when HTTP_REQUEST {

 

HTTP::redirect "[URI::protocol [HTTP::request]]://host.net[HTTP::uri]"

 

}

 

 

Unfortunately this doesn't work because [HTTP::request] does not include the protocol.

 

 

The URI::protocol command seems like it should do what I want - except I can't seem to figure out how to feed it the entire request (https://host.com/???).

 

 

The examples given for URI::protocol have it parsing a literal string - ie:

 

when HTTP_REQUEST {

 

set url "http://www.foo.com/app/file.ext"

 

proto will be "http"

 

set proto [URI::protocol $url]

 

}

 

 

This is the behavior I need - except I need to evaluate the incoming request, not a literal string.

 

 

What am I missing?

 

 

Thanks!

2 Replies

  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    The simplest way would be to test the port on which the request is received, and set the protocol string based on that.
    
    when HTTP_REQUEST {
      if {[string tolower [HTTP::host]] contains ".com"}{
        set host [string map -nocase {.com .net} [HTTP::host]]
        if {[TCP::local_port] == 443 {
          set proto "https"
        } else {
          set proto "http"
        }
        HTTP::redirect "$proto://$host[HTTP::uri]"
      }
    }
    (Since you mentioned you want this to work on multiple VS, I took the liberty of also adding a string map function to re-write any .com hostname to .net, which you can of course adjust to match your specific requirements.)

    HTH

    /deb
  • Thanks Deb - this is exactly what I needed! I was working on the same approach, but was (unsuccessfully) trying to use URI::port to determine the incoming port.

     

    (BTW for anyone using the code above - there's a missing "}" on line 4.)