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 ϕ1,ϕ2:Z→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 ϕ1 and ϕ2 agree. Then there is a nonconstant rational function g on X such that gϕ1=gϕ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 D-varieties and of Cantat's theorem to self-correspondences.