Forum Discussion

Fluidetom_12222's avatar
Oct 04, 2013

How to use only part of the uri ?

Hey,

I have a very simple iRule to redirect the traffic to another URL if the uri start with a string (in my case "/api").

    when HTTP_REQUEST {
      if { [HTTP::uri] starts_with "/api" } {
         HTTP::redirect "https://service.test.com:7002/Test"
      } else {
         HTTP::redirect "https://service.test.com:4443/LoginPage"
     }
    }

What I need to achieve now is that whatever is entred after /api must be appended to the HTTP::redirect. For example if a user enters https://service.test.com/api/SomethingElse, he must be redirected to https://service.test.com:7002/Test/SomethingElse.

So I need to

  1. capture whatever is after /api and add it after /Test
  2. the redirection must be transparent and the user must only see https://service.test.com/api/SomethingElse, and not the address he's redirected to.

How can I achieve this ?

Thanks in advance and have a nice day

3 Replies

  • Can you please try this.

     

    when HTTP_REQUEST {if { [string tolower [HTTP::uri]] starts_with "/abc" } { HTTP::uri [string map -nocase {"/abc" "/123/bac"} [HTTP::uri]] }}

     

  • Jurgen abd Samir have good examples. Here's what these might look like:

    when HTTP_REQUEST {
        if { [string tolower [HTTP::uri]] starts_with "/api" } {
            set newuri [string range [HTTP::uri] 4 end]
            HTTP::redirect "https://service.test.com:7002/Test$newuri"
        }
    }
    

    or

    when HTTP_REQUEST {
        if { [string tolower [HTTP::uri]] starts_with "/api" } {
            set newuri [string map -nocase {"/api" ""} [HTTP::uri]]
            HTTP::redirect "https://service.test.com:7002/Test$newuri"
        }
    }
    

    The most significant difference between these two examples is timing:

    % set uri "/api/foo/bar"    
    /api/foo/bar
    % puts [string range $uri 4 end]
    /foo/bar
    % puts [string map {"/api" ""} $uri]
    /foo/bar
    % time { set newuri [string range $uri 4 end] } 100000
    0.72165 microseconds per iteration
    % time { set newuri [string map {"/api" ""} $uri] } 100000
    0.80794 microseconds per iteration
    

    The string range command is only slightly faster.