Forum Discussion

BigBunk390_1180's avatar
BigBunk390_1180
Icon for Nimbostratus rankNimbostratus
May 20, 2014

iRule to redirect if value does not exist in cookie

Hello, I am trying to create an irule that check the users cookie for a value and if the value does not exist it should redirect the user to a different url.

 

Something similar to this:

 

when HTTP_REQUEST { if { not ( [HTTP::cookie value "VSession"] ) } { HTTP::redirect "differenturlloginpage.com" } }

 

Can someone please provide assistance?

 

3 Replies

  • Are you looking for the presence of a cookie in the request:

    when HTTP_REQUEST {    
        if { not ( [HTTP::cookie exists MYCOOKIE] ) } {        
            HTTP::redirect "https://differenturlloginpage.com"            
        }        
    }    
    

    Or for some value in a known cookie:

    when HTTP_REQUEST {    
        if { not ( [HTTP::cookie value MYCOOKIE] contains "somevalue" ) } {        
            HTTP::redirect "https://differenturlloginpage.com"
        }        
    }
    
  • I am looking for a value that should be present in a cookie we create upon login.

     

    On a Citrix Netscaler we have it configured like this: HTTP.REQ.COOKIE.CONTAINS("SMSESSION")

     

    so if the users cookie contains smsession the user can continue, but if the value does not exists it will redirect the user to the url.

     

  • A user can have multiple cookies and each of those cookies can have separate values, so to say "looking for a value that should be present in a cookie", would indicate that you're looking for a cookie's value, without specifying which cookie to inspect.

    I believe what that Netscaler config is doing is checking for the existence of the SMSESSION cookie itself in the request (not necessarily the value in that cookie), which in this case would be similar to the first example above.

    when HTTP_REQUEST {    
        if { not ( [HTTP::cookie exists SMSESSION] ) } {        
            HTTP::redirect "https://differenturlloginpage.com"            
        }        
    }