Technical Forum
Ask questions. Discover Answers.
cancel
Showing results for 
Search instead for 
Did you mean: 
Custom Alert Banner

Dynamic String Value iRule

nramadan
Nimbostratus
Nimbostratus

Hi everyone, 

From past days, I have been struggling an iRule that forward dynamic string from 1st URL to 2nd URL. For example

1st URL = https://example1.com/administration/data/value

2nd URL = http://example2.com/finance/data/value

when HTTP_REQUEST {
if {[string tolower [HTTP::host]] equals "https://example1.com"}{
if {[string tolower [HTTP::uri]] contains "/administration/data/"}{
HTTP::respond 302 noserver Location "http://example2.com[string map -nocase {"//finance/data/"} [HTTP::uri]]"
}
}
}

===================================================================

when HTTP_REQUEST {
set uri [HTTP::uri]
if { [HTTP::uri] contains "/administration/data/" } {
log local0. "Original URI: $uri"
HTTP::uri [string range [HTTP::uri] 400 end]
log local0. "Search Query: [HTTP::uri]"
HTTP::uri /finance/data/[HTTP::uri]
log local0. "New URI: [HTTP::uri]"
HTTP::redirect "http://example2.com[HTTP::uri]"
}
elseif { $uri starts_with "/administration/data/" } {
log local0. "Original URI: $uri"
HTTP::uri [string range [HTTP::uri] 400 end]
log local0. "Search Query: [HTTP::uri]"
HTTP::uri /finance/data/[HTTP::uri]
log local0. "New URI: [HTTP::uri]"
HTTP::redirect "http://example2.com[HTTP::uri]"
   }
}

I have used these 2 scripts, still got errors, any suggestions to fix this problem?

3 REPLIES 3

Yes, try the iRule below.

when HTTP_REQUEST {
    if { [string tolower [HTTP::host]] equals "example1.com" && [string tolower [HTTP::uri]] starts_with "/administration/data/value" } {
        HTTP::respond 302 noserver Location "https://example2.com[string map -nocase {"administration" "finance"} [HTTP::uri]]"
    }
}

Output:

[nvsluis@ansible ~]$ curl -v --resolve example1.com:80:192.168.178.201 http://example1.com/administration/data/value/blah
* Added example1.com:80:192.168.178.201 to DNS cache
* About to connect() to example1.com port 80 (#0)
*   Trying 192.168.178.201...
* Connected to example1.com (192.168.178.201) port 80 (#0)
> GET /administration/data/value/blah HTTP/1.1
> User-Agent: curl/7.29.0
> Host: example1.com
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 302 Moved Temporarily
< Location: https://example2.com/finance/data/value/blah
* HTTP/1.0 connection set to keep alive!
< Connection: Keep-Alive
< Content-Length: 0
<
* Connection #0 to host example1.com left intact
[nvsluis@ansible ~]$

Have fun,

     --Niels

Thanks for suggestion, not got errors anymore but the result is redirect to https://example1.com, is there any alternative scripts?

As you can see with the curl output in my previous reply, the iRule is working. Could you share a similar output with curl? If the iRule that I shared with you isn't working, the specifications on your side must be different, or I misunderstood them.