Forum Discussion
irule executes twice when refreshing a website
- Sep 13, 2016
Try unsetting your variables at the end of the iRule? Specifically...
unset $newSubdomain
Hi Lukasz,
the best approach to fix your iRule be to change the append newSubdomain $apiType .uat. syntax to set newSubdomain "${apiType}.uat.". In this case you don't need to manally issue a unset newSubdomain after each single web request. And no, this is not a bug. Its more a very important feature to have persistent session variables for the duration of an existing TCP connection.
As a general guideline I always recommend to use as little $variables in iRules as possibile. Try to set $variables only in the case you have to parse something more or less complex - but only if you need the results a second time or if you need those data in other (incompatible) iRule events.
Below is an example how you could improve the syntax of your code to use as little CPU overhead as possible. Its a combination of well selected commands and a consequent avoidance of unneccessary variables.
when HTTP_REQUEST {
log local0.debug "-------------------------------------------------------------"
log local0.debug "Incomming Request for: [HTTP::host][HTTP::path]"
if { ( [HTTP::path] starts_with "/api/" ) and ( [set apiType [getfield [HTTP::path] "/" 3]] ne "" ) } then {
log local0.debug "Set Host-Header to: ${apiType}.uat.[domain [HTTP::host] 2]"
HTTP::header replace "Host" "${apiType}.uat.[domain [HTTP::host] 2]"
log local0.debug "Set HTTP-Path to: /api[string range [HTTP::path] [string length "/api/$apiType"] end]"
HTTP::path "/api[string range [HTTP::path] [string length "/api/$apiType"] end]"
}
log local0.debug "Outgoing Request to: [HTTP::host][HTTP::path]"
log local0.debug "-------------------------------------------------------------"
}
Note: The iRule above uses a slightly different syntax which consumes much less CPU ressources (e.g. using [getfield [HTTP::path] "/" 3] instead of [lindex [split [HTTP::path] "/"] 2], using a substituted ${apiType}.uat.[domain [HTTP::host] 2] instead of [string map [list oldHost newHost] [HTTP::host]] and using a substituted /newpath[string range [HTTP::path] [string length /somepath] end] instead of [string map [list /somepath /newpath] [HTTP::path]]).
Cheers, Kai
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