Forum Discussion

MOHIT_125417's avatar
MOHIT_125417
Icon for Altostratus rankAltostratus
Mar 28, 2015

Explaination on the iRule

Hi,

 

Can somebody explain me about "string trimright" in the below iRule.

 

when HTTP_REQUEST { if { [string tolower [HTTP::uri]] ends_with "/" } { HTTP::redirect http://[HTTP::host][string trimright [HTTP::uri] "/"] } }

 

1 Reply

  • Hi MOHIT,

    the iRule will check an incoming request for the URI (
    /path?query
    ) for a trailing slash (
    /
    ).

    In case of a match it will remove all trailing slashes and redirect the client to the initial host and modified URI as follows:
     curl -I 'http://10.131.131.102/test/'
    HTTP/1.0 302 Found
    Location: http://10.131.131.102/test
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    

    In the example above the URI ends with a (

    /
    ) and the client will be redirected as indicated by the location header in the response.

     curl -I 'http://10.131.131.102/test'
    HTTP/1.0 200 OK
    Server: LB-NET
    Set-Cookie: JSESSIONID=427398920.293652780
    Set-Cookie: StaticCookie=lb-net_static
    Connection: close
    Content-Length: 1016
    

    In the second request the URI does not contain the trailing (

    /
    ) and the requested object will be served.

    To avoid a continuous loop (default requests will be of "/" and ends with a (
    /
    ) as well I recommend to change the iRule as follows:
    when HTTP_REQUEST {
        if { ([string tolower [HTTP::uri]] ends_with "/") and not ([HTTP::uri] equals "/") } {
            HTTP::redirect http://[HTTP::host][string trimright [HTTP::uri] "/"]
        }
    }
    

    You may also consider, to verify the

    [HTTP::path]
    only.

    Thanks, Stephan

    PS: Please lookup the tcl manpage for details on the "
    string trimright
    " command.