HTTP to HTTPS 301 redirection

Hi,

I have just written an iRule for the following purposes:

  1. http://sitename.com (without the WWW) should 301 redirect to https://www.sitename.com
  2. http://www.sitename.com should 301 redirect to https://www.sitename.com
  3. http://subdomain.sitename.com should 301 redirect to https://subdomain.sitename.com (and the same thing for all of our subdomains)

At the moment I’ve just written the iRule but I haven’t yet been able to test it - however I was just wondering if it looks correct? And also, as I’ve not got 9 subdomains listed on it - if I could achieve the desired results with a smaller / more efficient rule?!?

I’m here to learn so all feedback is welcome. Thanks in advance. Below is my iRule…

Code 
    when HTTP_REQUEST {
switch [string tolower [HTTP::host]] {
    "sitename.com" {
        HTTP::respond 301 Location "https://www.sitename.com[HTTP::uri]"
    }
    "subdomain1.sitename.com" {
        HTTP::respond 301 Location "https://subdomain1.sitename.com[HTTP::uri]"
    }
    "subdomain2.sitename.com" {
        HTTP::respond 301 Location "https://subdomain2.sitename.com[HTTP::uri]"
    }
    "subdomain3.sitename.com" {
        HTTP::respond 301 Location "https://subdomain3.sitename.com[HTTP::uri]"
    }
    "subdomain4.sitename.com" {
        HTTP::respond 301 Location "https://subdomain4.sitename.com[HTTP::uri]"
    }
    "subdomain5.sitename.com" {
        HTTP::respond 301 Location "https://subdomain5.sitename.com[HTTP::uri]"
    }
    "subdomain6.sitename.com" {
        HTTP::respond 301 Location "https://subdomain6.sitename.com[HTTP::uri]"
    }
    "subdomain7.sitename.com" {
        HTTP::respond 301 Location "https://subdomain7.sitename.com[HTTP::uri]"
    }
    "subdomain8.sitename.com" {
        HTTP::respond 301 Location "https://subdomain8.sitename.com[HTTP::uri]"
    }
    "subdomain9.sitename.com" {
        HTTP::respond 301 Location "https://subdomain9.sitename.com[HTTP::uri]"
    }
}

}

Hi 1000blocks,

the iRule below should catch them all…

when HTTP_REQUEST {
    if { [string tolower [HTTP::host]] eq "sitename.com" } then { 
        HTTP::respond 301 Location "https://www.sitename.com[HTTP::uri]" 
    } else { 
        HTTP::respond 301 Location "https://[getfield [HTTP::host] ":" 1][HTTP::uri]" 
    }
}

Cheers, Kai

Hi Kai,

We’ve just noticed an additional redirection scenario that isn’t working as planned…

https://sitename.com is doing a two 301 redirections to https://www.sitename.com

When we check the flow, it’s doing this…

301 Moved Permanently

301 Moved Permanently

200 OK

Ideally we want to get it down to showing as a single 301 redirect.

Would we need an additional rule to handle that specific scenario?

Any advice would be much appreciated (as always!!)