Rewrite iRule retaining part of URI

I’m trying to write an iRule that will redirect users from one URI to another - but the parameters passed in need to be retained in the redirect. However the URI is different.

I need it to receive:

http://mysite.com/maps.asp?SITEID=8&CTY=BER&PANITM=ADL&COID=2&CAT=HOT

and redirect to:

http://myredirectedsite.com/VoucherMap.aspx?SITEID=8&CTY=BER&PANITM=ADL&COID=2&CAT=HOT

I would be very grateful if someone knows how this can be done.

Many thanks,

Sam.

For one-off cases the replies so far are great and all you need. But if you need to do this in a general case, i.e. multiple substitutions on multiple VIPs, you can consider this rule which should be usable without modification due to its configurability:

Hi Aaron,

Many thanks for your reply - I have tried a version of this, I think my problem is that I want to redirect the host as well as performing a string map to the URI. I’ve tried this:

elseif { [HTTP::uri] contains “maps.asp”} {

HTTP::redirect http://[string map {mysite.com/maps.asp myredirectedsite.com/VoucherMap.aspx} [HTTP::uri]]

log LOCAL0.info “map rewrite”

}

but cannot get it working, is this because the string map is only applied to the uri, because of the elseif?

Sam.

I missed that you were needing to rewrite the host at the same time. Like kirkbauer noted, you can use the proxypass rule for multiple rewrites. Else for one-off changes to both the host and URI, here are updates for the examples I gave above…

You can use ‘string map’ (Click here) to rewrite /maps.asp to /VoucherMap.aspx within the complete URI and/or Host header strings.

You can rewrite the requested host and URI before the request is sent to the node with something like this:

when HTTP_REQUEST {
   HTTP::header replace Host [string map {oldhost newhost} [string tolower [HTTP::host]]]
   HTTP::uri [string map {/maps.asp /VoucherMap.aspx} [HTTP::uri]]
}

Else, if you do want to redirect the client to the new URI, you can use

when HTTP_REQUEST {
   if { [HTTP::uri] starts_with "/maps"}{
      HTTP::redirect "http://[string map {oldhost/maps.asp newhost/VoucherMap.aspx} [string tolower[HTTP::host]][HTTP::uri]]"
   }
}

If the web app is sending a 302 redirect, you can check in the HTTP_RESPONSE for a 30x response code and then look in the Location header for /maps and rewrite it to /VoucherMap.aspx.

Lastly, if the web application isn’t case sensitive for the URI, you should set the URI to lowercase before making comparisons. I used string tolower to do this for the host header value, as that is not case sensitive for any web app.

Aaron

Hi Aaron,

This worked perfectly, thank you very much for your time.