irule executes twice when refreshing a website
Hello.. I have a really odd problem, my irule script works exactly every second time. I'm working with APIs and on the backend, they are setup to use apiName.example.com/api/id. For the real world, We want to use api.example.com/api/apiName/id. I know this can be done with tools like TYK, however, at the moment we need a workaround using irules... My iRule:
when HTTP_REQUEST {
log local0. "-------------------------------------------------------------"
log local0. "Path [HTTP::path]"
if { not ([HTTP::path] starts_with "/api/") } { return }
set apiType [lindex [split [HTTP::path] "/"] 2]
log local0. "api type $apiType"
append newSubdomain $apiType .uat.
log local0. "New Subdomain $newSubdomain"
HTTP::header replace "Host" [string map [list "api-uat." $newSubdomain ] [HTTP::host]]
log local0. "-------True"
HTTP::path [string map [list "/api/$apiType" "/api"] [HTTP::path]]
log local0. "New Path [HTTP::host][HTTP::path]"
return
}
The above code works every second time... in the browser, I'm going to https://api-uat.example.com/api/inventory/demo and it should change into inventory.uat.example.com/api/demo which it does in the first request, but not the second one.. and every time I refresh it goes bad, good, bad, good.... no idea what I'm doing wrong, please help.
Path /api/inventory/demo
api type inventory
New Subdomain inventory.uat.
-------True
New Path inventory.uat.example.com/api/demo
-------------------------------------------------------------
Path /api/inventory/demo
api type inventory
New Subdomain inventory.uat.inventory.uat.
-------True
New Path inventory.uat.inventory.uat.example.com/api/demo
Try unsetting your variables at the end of the iRule? Specifically...
unset $newSubdomain