Forum Discussion

Brett_11764's avatar
Brett_11764
Icon for Nimbostratus rankNimbostratus
Nov 15, 2007

Redirection Woes

Hi,

 

 

I have written an iRule to redirect users based on the requests that they make. The thing I had to watch here was that the redirection was to the same VIP.

 

 

What I need:

 

 

1) User request does not include FQDN, redirect to original HTTP::host but include FQDN

 

2) If hostname includes specific text ("was"), continue without redirection

 

3) All other requests that do not include 'irj" in the URI, redirect to include the "irj".

 

 

So far the following seems to be working ok (any streamline suggestions would be welcome!):

 

 

when HTTP_REQUEST {

 

set uri [HTTP::uri]

 

set host [string tolower [HTTP::host]]

 

if {! ($host contains "domain.com") } {

 

if {( $uri equals "/" ) } {

 

if {! ($host contains "was" ) } {

 

HTTP::redirect "http://$host.domain.com/irj"

 

} else {

 

HTTP::redirect "http://$host.domain.com/$uri"

 

}

 

}

 

}

 

if {( $uri equals "/" ) } {

 

if {! ($host contains "was" ) } {

 

HTTP::redirect "http://$host/irj"

 

}

 

}

 

}

 

 

 

 

The problem I have though is that 1 in 10 requests that exclude the FQDN get a "page cannot be displayed". As soon as the user refreshes, the page loads. This problem does not exist when requesting the FQDN...

 

 

Anyone able to help?

 

 

 

  •  

    One thing to do in a case like this is to add logging statements so

     

    you can see the actual HTTP requests. In many cases this will

     

    allow you to figure out what is going wrong.

     

    log local0. "requested [HTTP::host][HTTP::uri]"

     

     

    From your URI (with "irj") I'm guessing this is SAP related. One

     

    thing SAP can do if it isn't configured correctly is insert port numbers

     

    for various URLs... which means your $host may also contain the

     

    port number. For example "xyz:80" which your code would redirect

     

    to "xyz:80.domain.com". That probably isn't what you want.

     

     

    So you might change your $host initialization statement to strip

     

    off any possible port suffix...

     

    set host [getfield [string tolower [HTTP::host]] ":" 1 ]

     

     

    Of course if you are intending to use something other than

     

    port 80, you will need to make more modifications to appropriately

     

    preserve the port number.

     

     

    On the streamlining issue... one thing they emphasized in the

     

    training course was that local variables are expensive/slow compared

     

    to just [HTTP::host]. If you think about it, using a local variable

     

    must actually involve allocating storage and looking the variable

     

    name up. I do use some local variable despite this when it makes

     

    my code much easier to read/maintain and I'm not concerned

     

    with heavy load.

     

     

    Hope this is of help.

     

     

    --Don
  • Hi,

     

     

    Thanks for your response. You are correct, this is a SAP deployment. There are services that run on different ports in this case, but these are known and handled separately.

     

     

    Point taken about using local variables, I will re-write and see how it goes. Will also add the logging to see if that will shed some light on the "page cannot be displayed issue".

     

     

    Thanks for help!
  • Ok, think I may have found the problem...

     

     

    A request that excluded the FQDN would be matched twice and therefore redirected twice. I have changed the rule by changing to an elseif and don't seem to have the problem any longer. Will have some load testing done and see what happens!

     

     

    Also removed some of the local variables...

     

     

    New rule:

     

     

    when HTTP_REQUEST {

     

    set host [string tolower [HTTP::host]]

     

    if {! ($host contains "domain.com") } {

     

    if {( [HTTP::uri] equals "/" ) } {

     

    if {! ($host contains "was" ) } {

     

    log local0. "FQDN! IRJ! WAS! Redirecting to http://$host.domain.com/irj"

     

    HTTP::redirect "http://$host.domain.com/irj"

     

    } else {

     

    log local0. "FQDN! IRJ! Redirecting to http://$host.domain.com[HTTP::uri]"

     

    HTTP::redirect "http://$host.domain.com[HTTP::uri]"

     

    }

     

    }

     

    }

     

    elseif {( [HTTP::uri] equals "/" ) } {

     

    if {! ($host contains "was" ) } {

     

    log local0. "IRJ! WAS! Redirecting to http://$host/irj"

     

    HTTP::redirect "http://$host/irj"

     

    }

     

    }

     

    }