Redirect 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

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”

}

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 :slight_smile:

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?

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.

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!!

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.

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

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.

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

Working like a charm, thanks so much again all for the help. I really appreciate it!

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 (404 Page not found | BIG-IP Documentation)

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!

Hi Brian,

You are already most of the way there with what you need, but you were performing the same process for both of them.

The “-” on the end tells the qualifying event to perform the same action as the next event, so you need to seperate the actions for each specific HTTP::uri.

In a switch statement the values in the “” are literal, so these are detecting the first part of the URI Path do differentiate them. The -glob allows for the use of the “*” Wildcard which will match anything else after what you are looking for.

“/vendorABC/status.asp*”

“/vendorXYZ/status.asp*”

Also, in your switch statement you are performing a string to lower for the comparison, so when you are performing a string tolower make sure that the values are all in lower case so that you get a match.

“/vendorABC/status.asp*”

“/vendorXYZ/status.asp*”

Should be:

“/vendorabc/status.asp*”

“/vendorxyz/status.asp*”

Try this:

when HTTP_REQUEST {
switch -glob [string tolower [HTTP::uri]] {
"/client/order.asp*" -
"/vendorabc/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]]
pool beta.res-direct.com_B2B_HTTP
}
"/vendorxyz/status.asp*" {
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
}
}
}

Hope this helps.

Perfect, this is working exactly as we expect. Thanks so much for the explanation of the rule elements Michael, that helps to understand exactly what’s happening.

Thanks to Kevin and Aaron as well for your help, you guys all rock!