Forum Discussion

Joe_Pipitone's avatar
Joe_Pipitone
Icon for Nimbostratus rankNimbostratus
Feb 04, 2015

Removing www and https redirect

I've applied the following iRule to a VIP on port 80. It successfully strips the www if there is one, and then redirects to the VIP on port 443.

I also want to apply this iRule to the VIP on port 443, however that would result in a redirect loop unless I remove the else statement. I'm trying to consolidate and prevent having two different irules per set of VIPs. Is this possible?

when HTTP_REQUEST {
  if {([string tolower [HTTP::host]] starts_with "www.")} {
    HTTP::redirect "https://[string range [HTTP::host] 4 end][HTTP::uri]"
  }
    else {
      HTTP::redirect https://[HTTP::host][HTTP::uri]
  }
}

2 Replies

  • Angelo's avatar
    Angelo
    Icon for Nimbostratus rankNimbostratus

    there should be a default i-rule that comes with the F5 that does this no need to write one

     

  • Hi, you can do the redirect conditionally via "elseif":

    when HTTP_REQUEST {
        if {([string tolower [HTTP::host]] starts_with "www.")} {
            HTTP::redirect "https://[string range [HTTP::host] 4 end][HTTP::uri]"
            return
        } elseif { [TCP::local_port] == 80 } {
            HTTP::redirect https://[HTTP::host][HTTP::uri]
            return
        }
    }
    

    Thanks, Stephan