In applications involving the injection of a fluid in a porous medium to displace another fluid, a main objective is the maximization of the displacement efficiency. For a fixed arrangement of injection and production points (sources and sinks), such optimization is possible by controling the injection rate policy. Despite its practical relevance, however, this aspect has received scant attention in the literature. In this paper, we provide a fundamental approach based on optimal control theory, for the simplified case when the fluids are miscible, of equal viscosity, and in the absence of dispersion and gravity effects. Both homogeneous and heterogeneous porous media are considered. From a fluid dynamics viewpoint, this is a problem in the deformation of material lines in porous media, as a function of time-varying injection rates. It is shown that the optimal injection policy that maximizes the displacement efficiency, at the time of arrival of the injected fluid, is of the “bang–bang” type, in which the rates take their extreme values in the range allowed. This result applies to both homogeneous and heterogeneous media. Examples in simple geometries and for various constraints are shown, illustrating the efficiency improvement over the conventional approach of constant rate injection. In the heterogeneous case, the effect of the permeability heterogeneity, particularly its spatial correlation structure, on diverting the flow paths, is analyzed. It is shown that bang–bang injection remains the optimal approach, compared to constant rate, particularly if they were both designed under the assumption that the medium was homogeneous. Experiments in a homogeneous Hele-Shaw cell are found to be in good agreement with the theory. © 2000 American Institute of Physics.