Forum Discussion

johnmervin_4578's avatar
johnmervin_4578
Icon for Altostratus rankAltostratus
May 31, 2017

http redirect irule

I am working on a irule which basically strips "/abchef" on the URI before loadbalancing to a pool here is my rule, the issue is if I remove "log local0" or "log" from this rule it did not work, is there a better way to do it

when HTTP_REQUEST {

        if { [HTTP::host] contains "testlab.john.com" and [HTTP::uri] starts_with "/abcdef/api/" }
        {
                    log local0[substr [HTTP::uri] 7]

pool testlab } }

  • Hi, I'm not sure what you need. You just need to log:

    when HTTP_REQUEST {
        if { [HTTP::host] contains "testlab.john.com" and [HTTP::uri] starts_with "/abcdef/api/" } {
            log local0. "[substr [HTTP::uri] 7]"
            pool testlab
        }
    }
    

    or you need to strip and rewrite uri before send to a server:

    when HTTP_REQUEST {
        if { [HTTP::host] contains "testlab.john.com" and [HTTP::uri] starts_with "/abcdef/api/" } {
            log local0. "[substr [HTTP::uri] 7]"
            HTTP::uri [substr [HTTP::uri] 7]
            pool testlab
        }
    }
    

    Regards.

  • John, I still can't see when are removing the complete log statement line, the iRule stops to work. But, if you are just trying to run "[substr [HTTP::uri] 7]" on this line, it's really wrong thing. You need to store back on HTTP::uri.

    HTTP::uri [substr [HTTP::uri] 7]
    pool testlab
    

    This way, on client side the URI should be:

    /abcdef/api/
    , and on server side, the URI should be:
    /api/

    Anyway, If you are facing issue when you do strip to uri value, and the traffic have an unexpected redirection because of the irule 1 execution, you can try to set priority to this irule. This way, it will execute before the iRule that you are rewriting the uri. e.g.

    irule 1
    when HTTP_REQUEST priority 300 { 
        if { [HTTP::host] contains "testlab.john.com" and [HTTP::uri] starts_with "/api/" }
        {
            HTTP::redirect "https://f5.com[HTTP::uri]"
        }
    }
    
    irule 2
    when HTTP_REQUEST {
        if { [HTTP::host] contains "testlab.john.com" and [HTTP::uri] starts_with "/abcdef/api/" } {
            HTTP::uri [substr [HTTP::uri] 7]
            pool testlab
        }
    }
    

    I think I have no more ideas to this.

    Respectfully.