  The following theorem, which includes as very special cases results of
Jouanolou and Hrushovski on algebraic $D$-varieties on the one hand, and of
Cantat on rational dynamics on the other, is established: Working over a field
of characteristic zero, suppose $\phi_1,\phi_2: Z \to X$ are dominant rational
maps from a (possibly nonreduced) irreducible scheme $Z$ of finite-type to an
algebraic variety $X$, with the property that there are infinitely many
hypersurfaces on $X$ whose scheme-theoretic inverse images under $\phi_1$ and
$\phi_2$ agree. Then there is a nonconstant rational function $g$ on $X$ such
that $g\phi_1=g\phi_2$. In the case when $Z$ is also reduced the
scheme-theoretic inverse image can be replaced by the proper transform. A
partial result is obtained in positive characteristic. Applications include an
extension of the Jouanolou-Hrushovski theorem to generalised algebraic
$\mathcal D$-varieties and of Cantat's theorem to self-correspondences.
