Forum Discussion

Thiyagu_343098's avatar
Thiyagu_343098
Icon for Nimbostratus rankNimbostratus
Jan 25, 2018

iRULE to remove the URI on LB

Hello All, I'm working on a request to remove the URI from the HTTP request and send the rest of the URL path as it is to the backend server: for example a request as and it should get routed to backend server as

 

 

the URI1 should get stripped on LB and it should get routed to the backend server

 

Any of your help or suggestion is appreciable.

 

Regards, Thiyagu

 

3 Replies

  • Hello Thiyagu,

    So, You can rewrite the requested URI before it's sent to the node with something like this:

    when HTTP_REQUEST {
        if {[HTTP::uri] starts_with "/URI1/"}{
        set uri [string range [HTTP::uri] 5 end]
        HTTP::uri $uri
    
        log local0. "new URI: https://[HTTP::host]$uri"
        }
        }
    

    Regards,

  • Hi Thiyagu,

    You can use this iRule to replace the first field in the URI path, regardless of what the field contains. It uses

    getfield
    which uses the forward slashes as delimiters and
    string map
    to replace what has been found, with a blank value.

    when HTTP_REQUEST {
        if {[HTTP::uri] contains "URI1"} {
            set uriField [getfield [HTTP::uri] / 2]
            set newUri [string map [list ${uriField}/ ""] [HTTP::uri]]
            HTTP::uri $newUri
        }
    }
    

    Getfield https://devcentral.f5.com/wiki/irules.getfield.ashx

    String map https://devcentral.f5.com/articles/irules-101-14-tcl-string-commands-part-2

  • thy this code:

     

    when HTTP_REQUEST {
        if {[scan [HTTP::uri] {/%[^/]%s} field1 newUri] == 2 } {
            switch $field1 {
                "URI1" {
                    pool POOL1
                    HTTP::uri $newUri
                }
                "URI2" {
                    pool POOL2
                    HTTP::uri $newUri
                }
            }
        }
    }