Forum Discussion
Brian_Kenworthy
Nimbostratus
Aug 30, 2012Redirect to New Page but Pass Query String Parameters
Hi All,
Need some advice on what an iRule would look like to do this....We are moving from legacy ASP listeners to .net listeners, but we don't want the client to change their endpoint until they are ready. So here's what it we are looking to do:
redirect requests destined for:
https://my.domain.com/vendor/status.asp?user=xxx&password=yyy
to
https://my.domain.com/vendor/status.aspx?user=xxx&password=yyy
I know how to redirect the whole URI, but am confused about how to change just one piece:
when HTTP_REQUEST {
HTTP::redirect "https://[HTTP::host][HTTP::uri]"
}
Thanks for your help in advance!!
BK
12 Replies
- Kevin_Stewart
Employee
Something like this should work:
when HTTP_REQUEST {
HTTP::uri [string map {".asp?" ".aspx?"} [HTTP::uri]]
}
This will silently replace the .asp? with .aspx? in the server-flowing URI. The client won't see this.
Or if you actually want to generate a redirect:
if { [HTTP::uri] contains ".asp?" } {
set uri [string map {".asp?" ".aspx?"} [HTTP::uri]]
HTTP::redirect "https://[HTTP::host]$uri"
} - Brian_Kenworthy
Nimbostratus
Thanks for the quick response Kevin! I like the transparent method, that's even better then a redirect. We don't what the client to see anything :)
I just got an update from development that they made now want to alter the URL a little more, so now it looks like this:
https://my.domain.com/vendor/status.asp?user=xxx&password=yyy
to
https://my.domain.com/listeners/status.aspx?user=xxx&password=yyy
How does this change things? - Michael_Yates
Nimbostratus
Hi Brian,
You can do multiple string map and replaces at the same time.
[string map {"value1" "replacevalue1with" "value2" "replacevalue2with" }
You could look for an [HTTP::uri] that contains both vendor and asp? and do something like this:when HTTP_REQUEST { if { [HTTP::uri] contains "vendor" && [HTTP::uri] contains ".asp?" } { set uri [string map {"vendor" "listeners" ".asp?" ".aspx?"} [HTTP::uri]] HTTP::redirect "https://[HTTP::host]$uri" } }
Hope this helps. - Brian_Kenworthy
Nimbostratus
Thanks all, I tested out each of the suggestions and it appears to be working great. One thing I wanted to do was log the original URI and the re-written URI, but I can't seem to be able to use HTTP::URI with HTTP_RESPONSE. It's probably something very simple....
when HTTP_REQUEST {
log local0. "OLD URI is --> [HTTP::host][HTTP::uri]"
HTTP::uri [string map {"vendor" "listeners" ".asp" ".ashx"} [HTTP::uri]]
}
when HTTP_RESPONSE {
log local0. [HTTP::URI] ??
}
Thanks for the help all!! - Michael_Yates
Nimbostratus
Hi Brian,
The information that you are looking for should be in the HTTP::header Location Field.
See these resource:
iRules Common Concepts
Log Http Headers
HTTP::header
Hope this helps. - hoolio
Cirrostratus
Are you trying to log the updated URI value? In pre-v11.0, the HTTP::uri was cached in the same event. So you could use something like this:
when HTTP_REQUEST {
log local0. "Rewriting [HTTP::uri] to [string map {"vendor" "listeners" ".asp" ".ashx"} [HTTP::uri]]"
HTTP::uri [string map {"vendor" "listeners" ".asp" ".ashx"} [HTTP::uri]]
}
Aaron - Brian_Kenworthy
Nimbostratus
Thanks Aaron, that is exactly what I was looking for. Michael, I tried looking at the HTTP::header location field and I wasn't seeing the new URL in the location field, not sure if I had it correct.
I have another wrinkle with this implementation...we currently have an iRule that is looking at the old URIs and directing the traffic to a different pool based on the URI value. I have added the re-write code to the existing iRule and it seems to work, however, the string tolower funtion doesn't appear to be working:
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::uri]] {
"/client/order.asp*" -
"/vendor/status.asp*" -
"/xml/order.asp*" -
"/xml/status.asp*" {
log local0. "Rewriting [HTTP::uri] to [string map {"Vendor" "Listeners" ".asp?" ".ashx?"} [HTTP::uri]]"
HTTP::uri [string map {"Vendor" "Listeners" ".asp?" ".ashx?"} [HTTP::uri]]
pool beta.res-direct.com_B2B_HTTP
}
We basically want to remove any case sensitivity on the URI.
Thanks so much for your help, this forum loaded with awesome folks. - hoolio
Cirrostratus
Can you try string map -nocase to do a case insensitive replacement?
log local0. "Rewriting [HTTP::uri] to [string map -nocase {"Vendor" "Listeners" ".asp?" ".ashx?"} [HTTP::uri]]"
HTTP::uri [string map -nocase {"Vendor" "Listeners" ".asp?" ".ashx?"} [HTTP::uri]]
Aaron - Brian_Kenworthy
Nimbostratus
Working like a charm, thanks so much again all for the help. I really appreciate it! - Brian_Kenworthy
Nimbostratus
Actually, I have another wrinkle to add...here's the code we're using and what is happening. Also note I had to replace the " " with {} per a configsync issue (http://support.f5.com/kb/en-us/solutions/public/12000/700/sol12795.html)
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::uri]] {
"/client/order.asp*" -
"/vendorABC/status.asp*" -
"/vendorXYZ/status.asp*" {
log local0. "Rewriting [HTTP::uri] to [string map -nocase {{VendorABC} {Listeners} {status.asp?} {abcstatus.ashx?}} [HTTP::uri]]"
HTTP::uri [string map -nocase {{VendorABC} {Listeners} {status.asp?} {abcstatus.ashx?}} [HTTP::uri]]
log local0. "Rewriting [HTTP::uri] to [string map -nocase {{VendorXYZ} {Listeners} {status.asp?} {xyzstatus.ashx?}} [HTTP::uri]]"
HTTP::uri [string map -nocase {{VendorXYZ} {Listeners} {status.asp?} {xyzstatus.ashx?}} [HTTP::uri]]
pool beta.res-direct.com_B2B_HTTP
}
So when a post hits /VendorABC/status.asp?... it gets routed correctly to /Listeners/abcstatus.ashx?...
But
The next post to /VendorXYZ/status.asp?... it gets an error becuase it's hitting /Listeners/abcstatus.ashx?... instead of /Listeners/xyzstatus.ashx?....
Because the asp page is named the same in both URIs, the first rule is detecting it and sending it to the wrong location. Is there a way to say when both /VendorABC/status.asp? are in the URI, direct to /Listeners/abcstatus.ashx? and when /VendorXYZ/status.asp? are in the URI, direct to /Listeners/xyzstatus.ashx?
I'm pulling my hair out with this one....Thanks in advance!
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
