05-Jun-2021 01:08
we need to rewrite the incoming request to a different endpoint and add some additional parameters to the URL.
The purpose of doing this is we don't want to expose the endpoint URL and the URL parameters to the browser. Below is an example:
incoming URL:
https://example.com/rec/widgets/item/McBq3g?=,&_br_uid_2=uid=59512300:v
1. Copy the url content from the incoming url after /widgets and add it to the rewrite url https://sample.com/api/v2/widgets/
2. append 2 additional url parameters &account_id=5036&domain_key=example at the end.
Final URL should look like this:
https://sample.com/api/v2/widgets/item/McBq3g?=,&_br_uid_2=uid=59512300:v&account_id=5036&domain_key=example
Please note the URL content after /widgets will change based on type of request but the logic of rewriting should be same. that is to copy everything after /widgets and append additional url parameters.
Solved! Go to Solution.
05-Jun-2021
14:14
- last edited on
21-Nov-2022
16:18
by
JimmyPackets
Assuming all incoming url starts_with /rec/widgets, you can try below code
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] starts_with "/rec/widgets" } {
HTTP::header replace "Host" "sample.com"
set new_uri "/api/v2/widgets[string range [HTTP::uri] 12 end]&account_id=5036&domain_key=example"
log local0. "new_uri: $new_uri"
HTTP::uri $new_uri
return
}
}
05-Jun-2021
14:14
- last edited on
21-Nov-2022
16:18
by
JimmyPackets
Assuming all incoming url starts_with /rec/widgets, you can try below code
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] starts_with "/rec/widgets" } {
HTTP::header replace "Host" "sample.com"
set new_uri "/api/v2/widgets[string range [HTTP::uri] 12 end]&account_id=5036&domain_key=example"
log local0. "new_uri: $new_uri"
HTTP::uri $new_uri
return
}
}
06-Jun-2021 06:18
Hey Sanjay,
Thanks for the quick reply!
Does the replacement host "sample.com" have to be in the same LTM? In our case, it's an external address.
Regards,
Mohan Kumar
06-Jun-2021 08:50
It can be hosted any where. You would need to modify the iRule above to create a pool for "sample.com" and use it with pool command before return statement.
Make sure pool is reachable from the LTM.
11-Jun-2021 03:15
Sure! I'll do that. Thank you!
I have one more query - What if the account ID keeps changing depends on the user (&account_id=5036), is there a way to make it dynamic?
11-Jun-2021 06:42
In your explanation, I don't see accountID is coming in incoming URL
https://example.com/rec/widgets/item/McBq3g?=,&_br_uid_2=uid=59512300:v
from where are you retriving this account ID? I thought it's static URI you are appending with a rewrite. Did you miss anything?
11-Jun-2021 10:59
Yes, You're right. I was wrong, sorry about that 🙂
We are testing the iRule, this weekend. I'll keep you posted.
Thank You so much for all your help!
16-Jun-2021 21:02
Sanjay,
The iRule is doing his job, by re-writing exactly, how we wanted. However, the page is not loading automatically (or using the incoming URL). When we try manually with a new URL (Enabled log, so I was able to get the new URI) in the browser it is loading. The pool is also reachable from LTM.
16-Jun-2021 23:54
you would need to capture tcpdump, enable logging in iRule to capture request and response and also review devolper tool logs from the browser to see what's happening.