Forum Discussion

cdougall_14195's avatar
Jun 24, 2011

Help cleaning up IRule redirect with URI rewrite.

So, right up front, I'm an old router guy, so scripting is not my forte. The request is to have requests that come into a VIP be redirected to another URL, change the URI, but maintain all the query information that follows... so, built a datagroup because since these are going to become more common, but this leads to a problem.

 

 

http://old.host.name/abc?xxxxxxx -> http://new.hostname.name/def?xxxxxxx

 

 

 

IRule:

 

when HTTP_REQUEST {

 

set urlredir [class match -value [HTTP::uri] starts_with DATA-GROUP]

 

if { $urlredir ne "" }{

 

set replace [class match -value $urlredir starts_with DATA-GROUP]

 

set redir [string map "$replace $urlredir" [HTTP::uri] ]

 

HTTP::redirect ""

 

}

 

}

 

 

Data external group:

 

"/abc" : = "new.host.name/def",

 

"new.host.name/def" : = "/abc",

 

 

So, what I'm looking for is a little help/direction so that I don't waste all the cycles doing the second call to the datagroup and don't have to have dual entries in the data group. It just seems really sloppy this way and I'm hoping that you guys can help me clean it up.

 

 

Thanks all,

 

 

Cameron

 

  • Hi Cameron,

     

     

    I understand redirecting old.host.name/abc to new.hostname.name/def. But why are you trying to redirect new.host.name/def back to /abc?

     

     

    Aaron
  •  

    Well, not really, and that's the part that I need some help with. The second lookup is used to cut out the part of the original URI that I want to remove.

     

    So, from the original uri /abc?xxxxxxx I want to remove just /abc and replace it with new.hostname.name/def. The problem that I'm running into is getting a way to match just the /abc from the original call.

     

    I've put some comments in below to show what I'm trying to do so that you guys can tell the right way to do it.

     

     

    Thanks all,

     

     

    Cameron

     

     

     

     

    IRule:

     

    when HTTP_REQUEST {

     

    set urlredir [class match -value [HTTP::uri] starts_with DATA-GROUP]

     

    [HTTP::uri] = old.host.name/abc?xxxxxxxxx

     

    urlredir = new.host.name/def

     

    if { $urlredir ne "" }{

     

    set replace [class match -value $urlredir starts_with DATA-GROUP]

     

    replace = old.host.name/abc without any of the passed arguments.

     

    set redir [string map "$replace $urlredir" [HTTP::uri] ]

     

    redir = the original uri where /abc is replaced with new.host.name/def

     

    and become new.host.name/def?xxxxxxx

     

    HTTP::redirect ""

     

    then redirected.

     

    }

     

    }

     

     

    Data external group:

     

    "/abc" : = "new.host.name/def",

     

    "new.host.name/def" : = "/abc",

     

     

     

  • It looks to me like you are trying to capture the HTTP::query and then verify the HTTP::path ends with your Data Group Value.

     

     

    Example: www.website.com/abc?value=100

     

     

    HTTP::uri = /abc?value=100 (Includes "/")

     

    HTTP::path = /abc (Includes "/")

     

    HTTP::query = value=100 (Does not include "?")

     

     

    You should try setting a variable to capture the query and then compare the path. Then redirect to the next destination and append the HTTP::query.
  • Hi cdougall, so what did you end up writing for your irule? I am trying to do this same thing. oldhost.domain.com/abc/ redirect to newhost.domain.com/def/