Forum Discussion

GioG's avatar
GioG
Icon for Nimbostratus rankNimbostratus
Jul 19, 2017

URI rewrite assistance

Hello all,

I am looking to hopefully do a simple irule uri rewrite, but this one is stumping me. I have a request to rewrite a uri from: http://somehost.com/reports/api/ to http://somehost.com/reports/api/v1/. Simple enough, and I got that to work with the irule below. But, they have several uri's that start with something different, so instead of reports it could be /admin/, /card/, /fee/ etc... I prefer the irule to be dynamic if possible and not have to list each directory out, and if possible use an asterisks or something for matching on */api/ and rewrite to */api/vi/. Hopefully this makes sense.

Those would look like: http://somehost.com/admin/api/ http://somehost.com/card/api/ http://somehost.com/fee/api/

So, is there a way to write an irule and use an asterrisks

when HTTP_REQUEST {    
 if { [string tolower [HTTP::uri]] starts_with "/reports/api/" } {
   set uri [string map -nocase {"/reports/api/" "/reports/api/v1/"} [HTTP::uri]]
   HTTP::uri $uri
 }
}

Any help is appreciated.

2 Replies

  • P_K's avatar
    P_K
    Icon for Altostratus rankAltostratus

    Try this.. should work

     

    when HTTP_REQUEST {

     

    if { [string tolower [HTTP::uri]] ends_with "/api/" }

     

    {

     

    HTTP::redirect http://[HTTP::host][HTTP::uri]v1/

     

    }

     

    }

     

    If your VIP is on port 443, change HTTP::redirect https://[HTTP::host][HTTP::uri]v1/

     

  • GioG's avatar
    GioG
    Icon for Nimbostratus rankNimbostratus

    None of that worked, but the "contains" lead me downt he correct path. Here is the final irule.

    when HTTP_REQUEST {    
        if { [string tolower [HTTP::uri]] contains "/api/" } {
        set uri [string map -nocase {"/api/" "/api/v1/"} [HTTP::uri]]
           HTTP::uri $uri
        }
    }