Forum Discussion
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
- capture whatever is after /api and add it after /Test
- 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
- Jurgen_Julius
Nimbostratus
You could use the 'string trimleft' to strip any leading characters.
- Samir_Jha_52506
Noctilucent
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]] }}
- Kevin_Stewart
Employee
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.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com