Forum Discussion
Greg_Bishop_104
Nimbostratus
Jun 27, 2011redirect subdomain to append to uri
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:
greg.domain.com --> www.domain.com/sites/greg
bob.domain.dom --> www.domain.com/sites/bob
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?
Thanks!
Greg Bishop
gbishop@celebratinghome.com
4 Replies
- hoolio
Cirrostratus
Hi Greg,
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.
Aaron - Greg_Bishop_104
Nimbostratus
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:
greg.domain.com --> www.domain.com/sites/greg
bob.domain.dom --> www.domain.com/sites/bob
www.greg.domain.com --> www.domain.com/sites/greg
www.bob.domain.com --> www.domain.com/sites/bob
when HTTP_REQUEST {
Check for a non-null length host header
if {[string match {*[a-zA-Z]*} [HTTP::host]]}{
Use the getfield command to parse the subdomain
set subdomain [getfield [HTTP::host] . 1]
HTTP::redirect http://www.domain.com/sites/$subdomain
}
}
Any help appreciated! Thanks! - nitass
Employee
e.g.[root@ve10:Active] config b virtual bar list virtual bar { snat automap pool foo destination 172.28.19.252:80 ip protocol 6 rules myrule profiles { http {} tcp {} } } [root@ve10:Active] config b rule myrule list rule myrule { when HTTP_REQUEST { set subdomain [string map {"www." "" ".domain.com" "" "domain.com" ""} [string tolower [HTTP::host]]] if { $subdomain ne "" } { HTTP::redirect "http://www.domain.com/site/$subdomain" } } } www.domain.com [root@centos251 ~] curl -I http://172.28.19.252 -H "Host: www.domain.com" HTTP/1.1 200 OK Date: Sat, 02 Mar 2013 00:22:03 GMT Server: Apache/2.2.3 (CentOS) Last-Modified: Sat, 27 Oct 2012 03:22:35 GMT ETag: "4183f3-59-f28f94c0" Accept-Ranges: bytes Content-Length: 89 Content-Type: text/html; charset=UTF-8 abc.domain.com [root@centos251 ~] curl -I http://172.28.19.252 -H "Host: abc.domain.com" HTTP/1.0 302 Found Location: http://www.domain.com/site/abc Server: BigIP Connection: Keep-Alive Content-Length: 0 www.abc.domain.com [root@centos251 ~] curl -I http://172.28.19.252 -H "Host: www.abc.domain.com" HTTP/1.0 302 Found Location: http://www.domain.com/site/abc Server: BigIP Connection: Keep-Alive Content-Length: 0 - Greg_Bishop_104
Nimbostratus
works like a charm. thank you so much!
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects