ssl_b0y_87395
Jul 18, 2011Nimbostratus
Collapsing Lots of URIs into one with 2 Irules
Hi
Long time-lurker, first time poster! We've got a customer who'd been using F5 for some time for tactical LTM stuff, when "from high" a requirement came to collapse their six or seven web applications into a single, uniform URL and move the static web-content to the "parent" organisations website. Physically doing this was just impossible given the logistics of three distinct web farms physically in three different locations, for various reasons.
I knew enough about irules to know that the F5 could make this happen, but I didn't have time to fudge about trying to get it to work so we engaged F5 PS to put together a solution for us. Essentially some URLS are proxied, some directed to local pools and some are straight redirects. There is a mix of HTTP and HTTPS, some applications available in both, some only in HTTPS. After 2 days of hair pulling and teeth gnashing we have a solution, which thus far works. I've obfuscated the code as much as possible, but hopefully it still makes sense..Apologies for the lack of comments in the code; it's how F5 PS supplied it :) I've had to make a couple of tweaks as additional requirements have dropped out of the woodwork, but it's a pretty much as it came..
It's been 15 years since I've done any programming in anger, my knowledge of f5 and irules comes entirely from DevCentral..So I'm posting this for the benefit of others and would be interested in any feedback, good, bad, indifferent!
Attached to the HTTP VS:
when HTTP_REQUEST {
Pull out a specific URL and change the host headers to send to the default pool (server-side HH logic in this case)
string tolower [HTTP::host]
if { [HTTP::host] contains "bl.newurl.co.uk"} {HTTP::header replace Host bl.oldurl.co.uk}
Otherwise this is the main Primary URL
Anyone accessing the root will be directed back out to the parent site
Anyone Accessing any of the nominated "applications" will be redirected to the HTTPS VS and the PATH appended.
Everything else, send to the default Pool but change to host header so the site accepts it and presents the correct content.
else {
switch -glob [string tolower [HTTP::path]] {
"/" { HTTP::redirect http://www.parentorg.co.uk/division }
"/favicon.ico" { HTTP::respond 404 }
"/intenrnalapp1*" -
"/externalapp1*" -
"/externalapp2*" -
"/externalapp3*" { HTTP::redirect https://www.newurl.co.uk[HTTP::uri] }
default { HTTP::header replace Host www.oldurl.co.uk }
}
}
}
when HTTP_RESPONSE {
re-write the responses to take out the refereces to the various old URLs/file locations and insert the new ones.
STREAM::disable
if { [HTTP::is_redirect] } {
if { [HTTP::header Location] starts_with "http" } {
HTTP::header replace Location [string map "[URI::host [HTTP::header Location]] www.newurl.co.uk" [HTTP::header Location]]
}
} elseif { [HTTP::header Content-Type] starts_with "text" } {
STREAM::expression {@www.oldurl.co.uk@www.newurl.co.uk@@www.oldurl2.co.uk@www.newurl.co.uk@@bl.oldurl.co.uk@bl.newurl.co.uk@@}
STREAM::enable
}
}