Redirect http://mysite.com and http://192.168.89.100 to http://www.mysite.com AND filter on matching URI before redirecting to HTTPS

Redirect http://mysite.com and http://192.168.89.100 to http://www.mysite.com AND filter on matching URI before redirecting to HTTPS I have one irule (IRULE A)in the http external virtual site http://www.mysite.com If the URI matches one of the three strings shown in IRULE A the client request gets redirected to https://www.mysite.com. It all works fine, no problems. Now I want to resolve another common problem for the same website when the client request is http://mysite.com OR http://192.168.89.100. I have done this for other websites on the F5 that do not need to filter the URI string (as in IRULE A). This was accomplished by using IRULE B. All three matches redirect to HTTPS://www.mysite.com, thereby avoiding the cert error. My thinking is; the HTTP request http://mysite.com and http://192.168.89.100 must be redirected to http://www.mysite.com before the SSL handshake to avoid the cert error AND then redirect to IRULEA or similar logic in the same irule . Only client requests that look like: http://www.mysite.com/checkcenter http://www.mysite.com/thisconnect http://www.mysite.com/mypolicies

should be redirected to: https://www.mysite.com/checkcenter https://www.mysite.com/thisconnect https://www.mysite.com/mypolicies

all other requests will remain http://www.mysite.com/whatever

It would be greatly appreciated if anyone with a skill level far superior to mine can figure this one out and provide me with the syntax to accomplish this. IRULE A when HTTP_REQUEST { switch -regexp [string tolower [HTTP::uri] ] { ^/checkcenter - ^/thisconnect - ^/mypolicies - { HTTP::redirect “https://[HTTP::host][HTTP::uri]” } default { pool web_s_pool} } }

IRULE B

when HTTP_REQUEST {

log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] to [HTTP::host][HTTP::uri]"
     switch -glob [string tolower [HTTP::host]] {
             "www.*" {
                      HTTP::redirect "https://[HTTP::host][HTTP::uri]"
                      log local0. "Redirecting www host to https://[HTTP::host][HTTP::uri]"
             }
             {*[a-z]*} {
                      HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"
                      log local0. "Redirecting non-www host to www.[HTTP::host][HTTP::uri]"
             }
             "192.168.89.100" {
                      HTTP::redirect "https://www.mysite.com"
                      log local0. "Redirecting null host to [IP::local_addr][HTTP::uri]"
             }
             default {
                      pool AppFW_pool
                      log local0. "Redirecting to default AppFW_pool"
             }
     }

}

George,

I am not sure i fully understand the desired logic but from what I gather from your explanation, you should just be able to add a nested switch to your existing iRule.

switch -glob [string tolower [HTTP::host]] {
  "www.mysite.com" {
    switch -glob [string tolower [HTTP::uri]] {
      "/checkcenter*" -
      "/thisconnect*" -
      "/mypolicies*" {
        HTTP::redirect "https://[HTTP::host][HTTP::uri]"
      } default {
        HTTP::redirect "http://[HTTP::host][HTTP::uri]"
      }
    }
  }
  "www.*" {
    HTTP::redirect "https://[HTTP::host][HTTP::uri]"
    log local0. "Redirecting www host to https://[HTTP::host][HTTP::uri]"
  }
  {*[a-z]*} {
    HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"
    log local0. "Redirecting non-www host to www.[HTTP::host][HTTP::uri]"
  }
  "192.168.89.100" {
    HTTP::redirect "https://www.mysite.com"
    log local0. "Redirecting null host to [IP::local_addr][HTTP::uri]"
  } default {
    pool AppFW_pool
    log local0. "Redirecting to default AppFW_pool"
  }
}

Is this what you are looking for?

Joe

Hi Joe:

If a user comes in http://192.168.89.100/checkcenter or http://mysite.com/mypolicies, I want him to be redirected to https://www.mysite.com/checkcenter. The problem is, if he goes in http://192.168.89.100/checkcenter and it redirects to https://192.168.89.100/checkcenter he will get a cert error. Therefore the http request has to get changed first from http://192.168.89.100/checkcenter to http://www.mysite.com/checkcenter before it gets redirected to https://www.mysite.com/checkcenter. The first 12 lines of your code seem to take care of the http://www.mysite.com client connections. If they come in http://www.mysite.com/checkcenter, off they go to the https redirect. If they they come in http://www.mysite.com, they do not go to https, but rather http://www.mysite.com (Please correct me if I am wrong on this last statement)

But it looks like the balance of the code is redirecting any entries that are malformed ie http://mysite.com or http://192.168.89.100 and redirecting them to https. They would only get redirected to https IF the URI was one of the three possibilites, /checkcenter, /mypolicies or /thisconnect. I hope this gives a better explanation and I simply not grossly misunderstanding your code.

Thanks

George

So are there multiple host names that resolve to the VIP that this iRule is on or is it only these three (mysite.com, www.mysite.com, 192.168.89.100)? If it is only these three then the irule does not really need to check for the hostname. It could just look at the URI:

switch -glob [string tolower [HTTP::uri]] {
  "/checkcenter*" -
  "/thisconnect*" -
  "/mypolicies*" {
    HTTP::redirect "https://www.mysite.com[HTTP::uri]"
  } default {
    HTTP::redirect "http://www.mysite.com[HTTP::uri]"
  }
}

Let me know if there are other host names that could hit this VIP/iRule and we can adjut the logic to account for those.

Joe

Joe:

I am starting to see the light. YES to your question, it is only these three (mysite.com,www.mysite.com and 192.168.89.100. I will test this and let you know how it worked. I greatly appreciate your effort and time.

Thanks

George

Joe: I tested out your code. We made some progress. Here are the results. I had to add the event and one bracket.

when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { “/checkcenter" - "/thisconnect” - “/mypolicies*” { HTTP::redirect “https://www.mysite.com[HTTP::uri]” } default { HTTP::redirect “http://www.mysite.com[HTTP::uri]” } } }

Results: YES=no problems, works fine. NO=browser just clocks, irule stats show irule is being accessed, but pool stats show request never gets that far.

http://www.mysite.com NO Reached external VIP, hit IRULE, did not get to the pool. http://mysite.com NO http://192.168.100.67 NO

http://www.mysite.com/thisconnect yes http:/mysite.com/thisconnect yes http://192.168.100.67 yes

I tried adding the default pool to the Resources default pool tab (using the code above), but got the same results.

Since I did not know how to add a default pool to your code and leave everything else intact, I removed one line and put in the default pool.

Add the default pool just to test out

when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { “/registrationcenter" - "/clientconnect” - “/mypolicies*” { HTTP::redirect “https://www.mysite.com[HTTP::uri]” } default { pool AppFW_pool } } }

Results

http://www.mysite.com yes

http://mysite.com yes http://192.168.100.67 NO

http://www.mysite.com/thisconnect yes - redirects to https, no cert issues http:/mysite.com/thisconnect yes http://192.168.100.67/thisconnect yes

I am getting a lot closer, only the IP request is failing. when it fails, the browser immediately returns an IE error page with “HTTP/1.1 service unavailable”. Do you have any code changes to suggest? If you added back the code line I pulled out in addition to adding in the default pool, do you think that would help. If you had a seperate line for redirecting the IP (http://192.168.100.67) to the host www.mysite.com, would this take care of the final problem?

Thanks

George

George,

How goes it? Did you get what you needed? OR are you still stuck? If you are getting an HTTP response from the server when you go to http://192.168.100.67 then it maybe be the web server is looking at the host header to determine what content to deliver.

Is the the “Service Unavailable” response coming from the server?

Joe

Joe:

I’am still stuck. I will find out if the response is coming from the server and let you know. Also I will do some more testing and try to give you a more consise description of what is and is not working so you can help. It would be a great relief to get this figured out. I think we are close.

George

Joe: You have been a great help, thanks for staying with this. Yes; to your prior question, the response is coming from the server. However the first irule below eliminates the problem.

I was able to get the correct results by using two irules. Can you combine the two to a single irule to get the same results? If not I’ll just use the two I have.

Objective: redirect http://mysite.com to http:/www.mysite.com redirect http://192.168.89.100 to http:/www.mysite.com redirect http://www.mysite.com/checkcenter to https://www.mysite.com/checkcenter

I can accomplish the objectives above using the two irules below in this order

when HTTP_REQUEST { if { [HTTP::host] eq “192.168.89.100” } {HTTP::redirect “http://www.mysite.com”} elseif { [HTTP::host] eq “mysite.com” } {HTTP::redirect “http://www.mysite.com” }}

when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { “/checkcenter" - "/thisconnect” - “/mypolicies*” { HTTP::redirect “https://www.test.hsb.com[HTTP::uri]” } default { pool AppFW_pool } } }

thanks

George

Joe: ignore the last message. I missed an important step. I’ll start over.

using irule 1 I am able to: redirect http://mysite.com to http:/www.mysite.com redirect http://192.168.89.100 to http:/www.mysite.com

using a irule 2 I am able to: redirect http://www.mysite.com/checkcenter to https://www.mysite.com/checkcenter

I need a solution to accomplish what the first and second irule does as just described in addition to: http://mysite.com/checkcenter redirect to https://www.mysite.com/checkcenter http://192.168.89.100/checkcenter redirect to https://www.mysite.com/checkcenter

irule 1 when HTTP_REQUEST { if { [HTTP::host] eq “192.168.89.100” } {HTTP::redirect “http://www.mysite.com”} elseif { [HTTP::host] eq “mysite.com” } {HTTP::redirect “http://www.mysite.com” }}

irule 2 when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] { “/checkcenter” - “/thisconnect” - “/mypolicies*” { HTTP::redirect “https://www.test.hsb.com[HTTP::uri]” } default { pool AppFW_pool } } }

If it takes two irules to get this right , that’s fine.

George,

This should do the trick:

Check URL to see if it should be redirected to https
switch -glob [string tolower [HTTP::uri]] {
  "/checkcenter*" -
  "/thisconnect*" -
  "/mypolicies*" {
    HTTP::redirect "https://www.mysite.com[HTTP::uri]"
  } default {
      If not an HTTPS-forced URL check Host header for www.mysite.com
      and redirect as needed
    if {!([string tolower [HTTP::host]] == "www.mysite.com"}{
      HTTP::redirect "http://www.mysite.com[HTTP::uri]"
    }
  }
}

Oops. Forgot a closing parenthesis on the if statement. “Edit button wasn’t working.”

Check URL to see if it should be redirected to https
switch -glob [string tolower [HTTP::uri]] {
  "/checkcenter*" -
  "/thisconnect*" -
  "/mypolicies*" {
    HTTP::redirect "https://www.mysite.com[HTTP::uri]"
  } default {
      If not an HTTPS-forced URL check Host header for www.mysite.com
      and redirect as needed
    if {!([string tolower [HTTP::host]] == "www.mysite.com")}{
      HTTP::redirect "http://www.mysite.com[HTTP::uri]"
    }
  }
}

Joe:

This worked!! I am all set, can’t thank you enough.

George