Forum Discussion
Peak_10_71174
Nimbostratus
Oct 27, 2009http redirect if no path match irule
I need to create an irule that accomplishes 2 things:
When incoming http requests for data.abc.com are received on a virtual server, they are redirected to www.abc.com/data. However, when a user requests data.abc.com/file.exe directly, that http request is then directed to the server pool on the load balancer that the virtual server is associated with. Below is my first attempt at writing this rule, but I am posting it on here because I am far from an expert in writing irules.
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::path]] {
"data.abc.com/file.exe" {
pool data_pool
log local0. "HTML hit - data_pool chosen"
}
default {
HTTP::redirect "http://www.abc.com/data"
log local0. "redirect_chosen."
}
}
}
I used the switch command in this attempt, but I am in no way certain that this is the best way to accomplish this.
5 Replies
- hoolio
Cirrostratus
HTTP::path will return the path in the URI (URI minus the query string). It will not include the host. HTTP::host will return the host header value. So you can remove data.abc.com from your rule and it should work fine.
And switch is a good way to do this.
Aaron - Matt_H_58911
Nimbostratus
I would like to propose another way. Will this work?
when HTTP_REQUEST {
if { [HTTP::path] equals "/" } {
HTTP::redirect "http://www.domian.com/base/"
}
if { [HTTP::path] equals $null } {
HTTP::redirect "http://www.domain.com/base/"
}
}
So if someone were to go to www.domain.com/downloadfile.exe the file would be downloaded
if someone were to go to www.domain.com or www.domain.com/ they would be redirected to www.domain.com/base/
Please advise. - hoolio
Cirrostratus
Hi Matt,
I'm not sure how the logic you've just posted relates to what you posted originally. Can you clarify what you want to have redirected and what you want to go to the pool? Do you just want to ensure a client request to / is redirected to the same host with a URI of /base/?
Does the domain you're redirecting the the client to resolve to the virtual server you're running the iRule on? If so, you'll need to be careful to avoid infinite redirects in your logic.
Also, the URI will never be null--all HTTP clients must specify a URI of / even if the user doesn't include a URI.
Aaron - Matt_H_58911
Nimbostratus
Hoolio,
Yes the main goal is to have everything going to directly to (from the first post) data.abc.com be redirected to www.abc.com/data. The hang up is that we do have content files in data.abc.com so http://data.abc.com/thisfile.exe needs to be downloaded from the pool and not redirected. My thought was I could simplify the first rule with what I posted as if it met the data.abc.com/ only criteria then it would be redirected, if not it’s a valid request to an explicit download.
Make sense? - JRahm
Admin
So, IF host = data.abc.com AND uri = /, redirect to www.abc.com/data?
If so, try this:when HTTP_REQUEST { if { [string tolower [HTTP::host]] eq "data.abc.com" && [HTTP::uri] eq "/" } { HTTP::redirect "http://www.[domain [HTTP::host] 2]/[getfield [HTTP::host] "." 1]" } }
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
