Hey guys, I’ve done a little bit with iRules but not very much. What I’m looking for is to redirect a large number of subdomains such as the following:
I cannot seem to find a variable that accounts for the part of the host before the dot (subdomain only) so I’m afraid it will take some trickery with length type calculations. Anyone have this one already hammered out?
You can use the string range, getfield or scan command to parse the subdomain from a host header value:
when HTTP_REQUEST {
Check for a non-null length host header
if {[string match {*[a-zA-Z]*} [HTTP::host]]}{
Use string commands to parse the subdomain
set subdomain [string range [HTTP::host] 0 [expr {[string first . [HTTP::host]] -1}]]
Use the getfield command to parse the subdomain
set subdomain [getfield [HTTP::host] . 1]
Use scan to parse the subdomain
scan [HTTP::host] {%[^.]} subdomain
}
}
I’d guess that getfield is probably the simplest and potentially the most efficient option to do this.
I’ve been using the below code flawlessly for a couple of years now (funny, I found this post again from Google searching). I could now use an update if someone would be so kind: I need to check and remove if someone has www. in front of the subdomain intended. Using my original example: