Forum Discussion

jonathan_west_2's avatar
jonathan_west_2
Icon for Nimbostratus rankNimbostratus
Sep 26, 2016

redirect based on uri with datagroup

I'm trying to write an irule that will extract parts of a uri, look it up in a few datagroups to find where to route to and wether an animal is valid and then insert the extracted uri into the http header. I've not had much luck with extracting the uri so I've tried using multiple datagroups but im stuck. I'm using version 11.

 

Eg:

 

192.168.1.2/monkey/v1.2/code/12345/*

 

will lookup in datagroup for code:

 

String 12345
Value   192.168.1.100

and another datagroup for animal:

 

string monkey string chimp string gibbon

 

and then redirect to 192.168.1.100 changing the uri to /monkey/v1.2/*

 

So far I've tried using class match, set and stringmatch but I'm not sure Im going in the right direction, any help gratefully received.

 

when HTTP_REQUEST {
 if { [ class match [string tolower [HTTP::uri]] contains animal ] and [ class match [string tolower [HTTP::uri]] contains code ] } {
  set original [HTTP::host]
  set new [class match -value [string tolower [HTTP::uri]] contains code]
  HTTP::host [string map "$original $new" [string tolower [HTTP::host]]]
  HTTP::redirect "http://$new[HTTP::uri]" }
 else {
  drop }
 }

1 Reply

  • If the URI doesn't change, you can use something like this:

     

    when HTTP_REQUEST {
    if { ( [class match [string tolower [HTTP::uri]] contains animal] and [class match [string tolower [HTTP::uri]] contains code] ) } {
    set row [class match [string tolower [HTTP::uri]] animal]
    if { not ($row eq "")} {
    HTTP::redirect http://[getfield $row " " 2][HTTP::uri]
    }
    }

    I am assuming uri should be /monkey/v1.2/ and not /monkey/v1.2/code/12345

     

    Try this:

     

    when HTTP_REQUEST {
     if { [ class match [string tolower [HTTP::uri]] contains animal ] and [ class match [string tolower [HTTP::uri]] contains code ] } {
      set original [HTTP::host]
      set new-host [class match -value [string tolower [HTTP::uri]] contains code]
      set new-uri [getfield [HTTP::uri] "code/" 1]
      HTTP::redirect "http://$new-host$new-uri" 
     else {
      drop }
     }