Forum Discussion

Sirisha_126707's avatar
Sirisha_126707
Icon for Nimbostratus rankNimbostratus
Mar 07, 2017

iRule Redirection

This is a redirection rule from https://xyz.co.uk to https://abc.co.uk But only 1 in 10 requests should redirect initially. Follwing is the iRule I have implemented but it gets redirected even in 20th and 23rd attempts as well . I need exactly 1 in 10 requests to be redirected. Can someone please help ?

 

when RULE_INIT { set static::cookie_name "abc-stg" ; set static::threshold 10 ;} when HTTP_REQUEST { set cookie_value -1 if {[HTTP::cookie exists $static::cookie_name]} { set cookie_value [HTTP::cookie value $static::cookie_name] if {![regexp {^\d+$} $cookie_value]} { set cookie_value -1 } elseif {$cookie_value < 0 || $cookie_value > 99} { set cookie_value -1 } } if {$cookie_value == -1} { set cookie_value [expr {int(rand()*100)}] HTTP::cookie insert name $static::cookie_name value $cookie_value } if {$cookie_value < $static::threshold} { HTTP::redirect "; } }

 

  • Why are using this code "set cookie_value [expr {int(rand()*100)}]" ?

     

    If the cookie does not exist you set it to 1, if exists you increment it. When it reaches 11, you save it as 1. When the cookie value is 1 you redirect.

     

  • You should try posting this with better formatting-

     

    "Preformatted Code Blocks

     

    Indent every line of a code block by at least 4 spaces or 1 tab."

     

  • Hi,

     

    you can try this code (not tested):

     

    when RULE_INIT {
        set static::cookie_name "abc-stg" ;
        set static::threshold 10 ;
        set static_timeout 3600
    }
    
    when HTTP_REQUEST {
        if {set value [table incr $static::cookie_name $static_timeout] >= $static::threshold} {
        table set  $static::cookie_name 0 $static_timeout
        HTTP::redirect "https://abc.co.uk"
        }
    }