Forum Discussion
Handling www with host name redirects in iRule
I'm trying to figure out how I can handle the following situation.
The section of the iRule below is for redirecting to a different URL based on a hostname. Our organization has purchased multiple domain names that are more or less used as shortcuts to a specific URL.
For example: companydepartment.com -> http://www.company.com/department/index.html
I'm using data groups to facilitate this with a name -> value in a string data group. In trying to optimize the code for this, I've ran into a snag when they want to use www.companydepartment.com.
I have to create a duplicate entry for www.companydepartment.com -> http://www.company.com/department/index.html
How can I handle this traffic so that both hostname with and without www. will be matched, and also with the understanding that there may be a subdomain to a different URL in some cases www.training.companydepartment.com -> http://www.company.com/department/training.html
if {[class match [HTTP::host] eq group_80_host_redirect]} {
HTTP::redirect [class match -value [HTTP::host] eq group_80_host_redirect]
}
Data group example
ltm data-group internal group_80_host_redirect {
records {
company.com {
data http://www.company.com
}
companydepartment.com {
data http://www.company.com/department/index.html
}
training.companydepartment.com {
data http://www.company.com/department/training.html
}
}
type string
}
- Tim_K_92675Cirrostratus
If you consistently maintain your data group names without 'www.' then you could pull in the requested hostname, strip off www. if it exists, and then run it through the data group. Something like this (untested)...
when HTTP_REQUEST { if { [string range [HTTP::host] 0 3] eq "www." } { set hostname [string range [HTTP::host] 4 end] } else { set hostname [HTTP::host] } if {[class match $hostname eq group_80_host_redirect]} { HTTP::redirect [class match -value [HTTP::host] eq group_80_host_redirect] } }
Hi Cody,
you may try the performance optimized snippet below. It would require just a single data group query to check for existence and to retrieve the location value...
when HTTP_REQUEST { if { [HTTP::host] starts_with "www." } then { if { [set location [class match -value [string range [HTTP::host] 4 end] eq group_80_host_redirect]] ne "" } then { HTTP::redirect $location return } } else { if { [set location [class match -value [HTTP::host] eq group_80_host_redirect]] ne "" } then { HTTP::redirect $location return } } HTTP::redirect "https://www.company.com/unknown_hostname/error.html?host=[URI::encode [HTTP::host]]" }
Cheers, Kai
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com