I am wanting to redirect certain hosts to HTTPS for offload. my iRule is working for hosts within the domain (e.g. ) but not for base domain host (e.g. domain.com).
The below iRule is working for www and uat, but not the base domain. Any assistance for what is wrong.
when HTTP_REQUEST { switch [string tolower [HTTP::host]] { “domain.com” { HTTP::respond 301 Location “https://domain.com[HTTP::uri]”} "; { HTTP::respond 301 Location “https://www.domain.com[HTTP::uri]”} “uat.domain.com” { HTTP::respond 301 Location “https://uat.domain.com[HTTP::uri]”} default {pool default_pool} } }
I had to remove the “;” after ";, but otherwise your iRule syntax works as expected for me.
You could try using
curl
to test against the IP address of your VIP to see if you get the desired behavior.
curl -iv -H "Host: domain.com" http:///
If it works, compare against the output of:
curl -iv http://domain.com/
While your iRule syntax isn’t wrong, it could be re-written as:
when HTTP_REQUEST {
switch [string tolower [HTTP::host]] {
"domain.com" -
"www.domain.com" -
"uat.domain.com" {
HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]"
}
default {
pool default_pool
}
}
}
Expanding on from DannisJann’s answer, I recommend using
getfield
around the
HTTP::host
command to remove any trailing port e.g.
curl -iv http://domain.com:80/
should still match in the switch statement.
Also you do not need to use the
pool
command as you have not overwritten the Virtual Server configured pool earlier in the iRule.
If you have used the
pool
command in an earlier iRule then you may want to put your pool selection back in but recommend using
pool [LB::server pool]
instead of the pool by name.
when HTTP_REQUEST {
switch [string tolower [getfield [HTTP::host] : 1]] {
"domain.com" -
"www.domain.com" -
"uat.domain.com" {
HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]"
}
default {
return
}
}
}