Forum Discussion

Jim_Mathers_131's avatar
Jim_Mathers_131
Icon for Nimbostratus rankNimbostratus
Oct 27, 2014

simplest method to strip "www." from Host field then HTTPS redirect

Hey folks, my requirements are simple:

  1. If the Host header in the HTTP request starts with “www.”, strip “www.” then redirect to HTTPS.

  2. If “www.” is not in the Host header, preserve the Host header and redirect to HTTPS.

This is what I came up with:

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]
  }
}  

Best way? Looking for the simplest possible solution. I'm new to iRules.