Hi first thing, if your expecting this rule to fire when the user requests page "/moo/dog.html?id=99" then you need to change the rewrite ^.*/index.php for ^.*/moo/dog.html$, unless there are other rules already in place, that may muddy things.
I'm far from a nginx expert, but am I correct in thinking the $http_HEADER variable contains the full HTTP header, including the referer, so it may be worth switching the two to see if it makes a difference. Also the syntax below may be closer to what you want
if ($http_referer ~* (moo/cow.html) ) {
set $test a;
}
if ($args !~* "id=15"){
set $test "${test}b";
}
if ($test = ab ) {
rewrite ^.*/moo/dog.html$
http://target.electronicgroups.com/moo/cat.html?id=15 last;
}