Forum Discussion

Rajesh_74831's avatar
Rajesh_74831
Icon for Nimbostratus rankNimbostratus
Nov 12, 2014

Want to remove "/" at the end of uri

Hi All,

   We wanted to create an irule which should remove "/" from the uri.

   Something like if user tries /">http://abc.com/xxxx/ it should be redirected to http://abc.com/xxxx .

   I tried following irule but its not working.

                        when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] ends_with "/" } {
    log local0. "Incoming request = [HTTP::host][HTTP::uri]"
    set uri [string map -nocase {"/" ""} [HTTP::uri]]
    log local0. "New URI = $uri"
    HTTP::uri $uri
}

}

Thanks -Rajesh

7 Replies

  • shaggy's avatar
    shaggy
    Icon for Nimbostratus rankNimbostratus

    I think "string trimright" is what you need instead of "string map"

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

    note - this currently also matches a lone "/", so http://abc.com/ will be redirected to http://abc.com

    Do you want to redirect the user to the new URI or pass the modified URI to the pool member?

  • worked like a charm ..... thanx a ton.

     

    For your question of the URI modification of the actual url also works for me ...

     

    Thanks

     

    -Rajesh

     

  • I was a bit quick to reply back ... looks like it worked fine for when the URI is present but when accessing the actual url its going in a redirect loop ...

     

    Any suggestions.

     

    • shaggy's avatar
      shaggy
      Icon for Nimbostratus rankNimbostratus
      can you elaborate? does the actual URL redirect to another URL with a trailing "/"? Can you provide an example of the redirect loop (curl results could be handy)? I had no issues with the iRule itself - it sends a redirect in response to any requested URI that has a trailing / (below). If you just want the iRule to modify the URI submitted to the pool member instead of a redirect back to the user, you can use HTTP::uri [string trimright [HTTP::uri] "/"] instead of HTTP::redirect http://[HTTP::host][string trimright [HTTP::uri] "/"] > GET /test/ HTTP/1.1 > User-Agent: curl/7.37.1 > Host: 1.1.1.1 > Accept: */* > * HTTP 1.0, assume close after body < HTTP/1.0 302 Found < Location: http://1.1.1.1/test < Server: BigIP * HTTP/1.0 connection set to keep alive! < Connection: Keep-Alive < Content-Length: 0 < * Connection 0 to host 1.1.1.1 left intact * Issue another request to this URL: 'http://1.1.1.1/test' * Found bundle for host 1.1.1.1: * Re-using existing connection! (0) with host 1.1.1.1 * Connected to 1.1.1.1 (127.0.0.1) port 80 (0) > GET /test HTTP/1.0 > User-Agent: curl/7.37.1 > Host: 1.1.1.1 > Accept: */* > < HTTP/1.1 503 Service Temporarily Unavailable
  • $ curl -v qa2.opensky.com * Rebuilt URL to: qa2.opensky.com/ * About to connect() to qa2.opensky.com port 80 (0) * Trying 192.168.71.154... * Adding handle: conn: 0x7fc87b80f800 * Adding handle: send: 0 * Adding handle: recv: 0 * Curl_addHandleToPipeline: length: 1 * - Conn 0 (0x7fc87b80f800) send_pipe: 1, recv_pipe: 0 * Connected to qa2.opensky.com (192.168.71.154) port 80 (0)

     

    GET / HTTP/1.1 User-Agent: curl/7.33.0 Host: qa2.opensky.com Accept: /

     

    • HTTP 1.0, assume close after body < HTTP/1.0 302 Found < Location: https://qa2.opensky.com/ < Server: BigIP
    • HTTP/1.0 connection set to keep alive! < Connection: Keep-Alive < Content-Length: 0 <
    • Connection 0 to host qa2.opensky.com left intact
  • bottom line is http://qa2.opensky.com/ should not be redirected or trimmed and any other uri associated with / should be trimmed.

     

    I am new to the application and irules, so bear with my ignorance if any.

     

    Thanks -Rajesh

     

  • shaggy's avatar
    shaggy
    Icon for Nimbostratus rankNimbostratus

    This ought do it - if the URI is more than one character in length and the URI ends with /, the iRule will redirect to the same URI with all right-most /'s stripped. abc.com/test/// will redirect to abc.com/test

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