Forum Discussion
redirect based on host and uri
Hi i have the below irule which is working fine. I'm trying to make it more efficient and only issue one redirect if the request came in as https://oldsite.com/. Currently what happens is i get the first redirect, then the second one when the uri matches /.
The existing irule:
when HTTP_REQUEST {
if {[string tolower [HTTP::host]] eq "oldsite.com"} {
HTTP::redirect "https://newsite.com[HTTP::uri]"
}
elseif {[string tolower [HTTP::uri]] eq "/"} {
HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"
}
}
The new iRule I'm attempting to get working is:
when HTTP_REQUEST {
if { ([string tolower [HTTP::host]] eq "oldsite.com") and ([string tolower [HTTP::uri]] eq "/") } {
HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"
}
elseif {[string tolower [HTTP::uri]] eq "/"} {
HTTP::redirect "https://newsite.com/identitymanagement/default.aspx"
}
}
This fixes the multiple redirects.
The problem i'm having is if i get a request for https://oldsite.com/path1, i still need to redirect to the https://newsite.com/path1. I can't figure out how to also take into account this. I've also tried converting this to a switch statement, but wasn't able to find a way to check both http::host and http::uri to formulate the correct redirect.
Thanks,
3 Replies
- Michael_193050
Nimbostratus
I added this and it's working fine. Let me know if there is a more efficient way of doing this thru a switch statement.
when HTTP_REQUEST { if { ([string tolower [HTTP::host]] eq "oldsite.com") and ([string tolower [HTTP::uri]] eq "/") } { HTTP::redirect "https://newsite.com/identitymanagement/default.aspx" } elseif {[string tolower [HTTP::uri]] eq "/"} { HTTP::redirect "https://newsite.com/identitymanagement/default.aspx" } elseif {[string tolower [HTTP::host]] eq "oldsite.com"} { HTTP::redirect "https://newsite.com[HTTP::uri]" } }Thanks
- VernonWells
Employee
I believe you can actually get what you need from the first iRule, but simply invert the cases, as in:
when HTTP_REQUEST { if { [HTTP::uri] eq "/" } { HTTP::redirect "https://newsite.com/identitymanagement/default.aspx" } elseif { [string tolower [HTTP::host]] eq "oldsite.com" } { HTTP::redirect "https://newsite.com[HTTP::uri]" } }That way, if the request is "http[s]://oldsite.com/" or "http[s]://newsite.com/", the redirect will be to "https://newsite.com/identitymanagement/default.aspx". Otherwise, a redirect occurs if and only if the Host header is "oldsite.com". (Notice that I removed the
on thestring tolower
verification, since it isn't needed for this comparison case).HTTP::uriInstead of using an iRule, however, you should consider employing Local Traffic Policies if you are using 11.4 or above, or HTTP Classes if you are using a version lower than that:
Generally, these mechanisms are more efficient than an iRule.
- Michael_193050
Nimbostratus
Thanks for the response, switching the cases worked exactly like i needed. I'm familiar with the policies, i just really haven't explored how to accomplish the same things i'm used to doing in irules.
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