Forum Discussion

Kenny_Van_73892's avatar
Kenny_Van_73892
Icon for Nimbostratus rankNimbostratus
Dec 01, 2005

redirect and hostheader

Hello all,

 

 

I need your help with this irule. I'd like to direct http traffic based on host header but I'd also like to use redirect http traffic to https if someone happens to mistype without "s".

 

 

when HTTP_REQUEST {

 

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

 

if { [HTTP::header Host] contains "test1" } {

 

pool Test1

 

}

 

elseif {[HTTP::header Host] contains "test2"} {

 

pool Test2

 

}

 

else { discard }

 

}

 

 

When I tested either single irule (redirect and hostheader), then everything worked just fine but when I combined them into a single irule as showed, then the http traffic got freaked out and didn't know where to go. Did I do something wrong here? Thanks all.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    The problem with combining any other rule with a rule that does indescriminant redirecting is that you're almost sure to get yourself in a loop.

    So, what you want to do is put an if around that redirect. Something that makes sure they're coming in on port 80 before redirecting them to port 443 and, consequently, doesn't redirect them at all of they're coming in on port 443.

    Also, the HTTP::host variable is pre-set, and serves the same function as HTTP::header Host, so I've replaced that below as well.

    Try this:

    
    when HTTP_REQUEST {
      if{ [TCP::local_port] == 80 } {  
        HTTP::redirect "https://[HTTP::host][HTTP::uri]"
        return
      }
      
      if { [HTTP::host] contains "test1" } {
        pool Test1
      } elseif { [HTTP::host] contains "test2" } {
        pool Test2
      } else { 
        discard 
      }
    }

    HTH,

    -Colin
  • Thanks Colin.

     

     

    I tried your rule but somehow the redirect to https still didn't work. I checked without https and everything worked just right.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    When you say the redirect didn't work...what do you mean? How did it behave?

     

     

    Keep in mind that if you're coming in on HTTPS, then the BIG-IP will need to be configured to terminate SSL, and this VIP will need to be configured for all ports, so that it can receive both the HTTP and HTTPS traffic (since you're redirecting to the same host from 80 to 443).

     

     

    -Colin