Forum Discussion

ikkut23's avatar
ikkut23
Icon for Altostratus rankAltostratus
Sep 21, 2024

Help with iRule Proxy

Hi team,

I’m working on an iRule where I need to replace the path /admin with the root / and forward the request to the appropriate pool. However, I’m encountering issues with the rule, and it doesn't seem to work as expected.

Here’s the first version I implemented:

 

when HTTP_REQUEST

 { if {[string tolower [HTTP::host]] equals "test.com" and [HTTP::path] starts_with "/admin"} { HTTP::path [string map -nocase {"/admin" "/"} [HTTP::path]] pool POOL-A #log local0.info "Client Address --> [IP::client_addr] | Path: [HTTP::path] | Pool: POOL-A" } else { pool POOL-B #log local0.info "Client Address --> [IP::client_addr] | Path: [HTTP::path] | Pool: POOL-B" } }

After some research, I saw that HTTP::path might need to be changed to HTTP::uri. I tried this version:

 

when HTTP_REQUEST { # Log the original URI for debugging log local0. "Original URI: [HTTP::uri]" # Check if the URI starts with "/admin" if {[HTTP::uri] starts_with "/admin"} { # Modify the URI by replacing "/admin" with "/" set new_uri [string map {"/admin" "/"} [HTTP::uri]] HTTP::uri $new_uri # Log the modified URI for debugging log local0. "Modified URI: [HTTP::uri]" # Forward the request to the appropriate pool pool POOL-A } else { # Log default traffic for debugging log local0. "Default traffic - URI: [HTTP::uri], Pool: POOL-B" # Forward to the default pool pool POOL-B } }

Issue:

Neither version seems to work. When I test requests to /admin, the path replacement does not happen as expected or The replace of path does not allow me to reach any subfolders after root “/” (ex. help, etc etc) and on these objects we faced 404 not found error.Could someone point out what I might be missing or any best practices for this kind of path manipulation?

Thanks!

  • when HTTP_REQUEST {

     
    if { [HTTP::path] equals "/" } {

     
    HTTP::redirect "http://www.domian.com/base/"

     

     

  • Try the following approach, ensuring proper handling of both the path replacement and subfolder routing:

    when HTTP_REQUEST {
        # Log the original URI for debugging
        log local0. "Original URI: [HTTP::uri]"

        # Check if the URI starts with "/admin"
        if {[HTTP::path] starts_with "/admin"} {
            # Replace only the initial "/admin" with "/"
            set new_path [string map {"/admin" "/"} [HTTP::path]]
            set new_uri "${new_path}[HTTP::query]"
            
            # Update the URI to reflect the modified path
            HTTP::uri $new_uri

            # Log the modified URI for debugging
            log local0. "Modified URI: [HTTP::uri]"

            # Forward the request to POOL-A
            pool POOL-A
        } else {
            # Log default traffic for debugging
            log local0. "Default traffic - URI: [HTTP::uri], Pool: POOL-B"

            # Forward to the default pool
            pool POOL-B
        }
    }


    Fintech Companies