Forum Discussion

Little_Daemon_1's avatar
Little_Daemon_1
Icon for Nimbostratus rankNimbostratus
Nov 13, 2013

Redirect initial request to a different URL

Guys, I have an iRule that looks like this:

            when HTTP_REQUEST {
              if { [HTTP::uri] starts_with "/context/ws" } {
                   pool pool_8080
               } elseif { not ([HTTP::uri] starts_with "/context") } { 
                   HTTP::uri /context
                   pool pool_80 
               } else {
                   pool pool_80 
               } 
            }

What this essentially does is:

Due to a design issue, the application is not able to handle the first time access to the page eg. http://domain.com/context/first.jsp Subsequent accesses (once authenticated) will work. So the first time the user comes in with the URL http://domain.com/context/first.jsp we want to redirect the user to a different URL like http://domain.com/context/login.jsp

How do we trap the first request in the iRule? [HTTP::uri] starts_with "/context/first.jsp" will trap all requests but we just want to trap only the very first one (when the user is not authenticated yet) and then redirect to the login URL. What would be the CONDITION in the iRule below so that it will modify the URI only in the case of the very first request and not subsequent requests?


    if { [HTTP::uri] starts_with "/context/first.jsp" AND (___CONDITION___)} {
               HTTP::uri /context/login.jsp
               pool pool_80
    }
  

Any thoughts would be much appreciated.

Thanks in advance.

3 Replies

  • Just set a variable and check for that first, for example;

    when CLIENT_CONNECTED {
     set firsttime 1
     }
    when HTTP_REQUEST {
     if { ([HTTP::uri] starts_with "/context/first.jsp"} && ($firsttime equals 1) } {
      set firsttime 0
      HTTP::uri /context/login.jsp
               pool pool_80
    }
    

    There's probably a better way but all I can think of right now. Not sure if it'll break if a second TCP connection is opened.

  • This won't work. This event is triggered for every TCP connection and as a result it is triggered for every resource on the page. Any other thoughts guys?

     

  • Let's assume that the request for "/context/first.jsp" is followed by subsequent calls to the same URL, and that all happen within the same TCP session. If that is the case, then Steve's iRule above should fill the requirement. The condition is triggered ONLY if the request is for "/context/first.jsp", AND ONLY if this is the first request in the TCP session.

    Otherwise, given the stateless nature of HTTP, you'd probably have to resort to a state mechanism like cookies to prevent further triggering of the condition. Example:

    when HTTP_REQUEST {
        if { not ( [HTTP::cookie exists STATECOOKIE] ) } {
             First time through
            set setcookie 1
            if { [string tolower [HTTP::uri]] starts_with "/context/first.jsp" } {
                HTTP::uri /context/login.jsp
                pool pool_80
            } else {
                 your other logic
            }
        }
    }
    when HTTP_RESPONSE {
         will insert the cookie on first response
        if { [info exists setcookie] } {
            unset setcookie
            HTTP::cookie insert name "STATECOOKIE" value "1" path "/"
        }
    }