  We determine the Riemannian manifolds for which the group of exact volume
preserving diffeomorphisms is a totally geodesic subgroup of the group of
volume preserving diffeomorphisms, considering right invariant $L^2$-metrics.
The same is done for the subgroup of Hamiltonian diffeomorphisms as a subgroup
of the group of symplectic diffeomorphisms in the K\"ahler case. These are
special cases of totally geodesic subgroups of diffeomorphisms with Lie
algebras big enough to detect the vanishing of a symmetric 2-tensor field.
