We study the Josephson effect in a superconductor--ferromagnet--superconductor (SFS) junction with ferromagnetic domains of non-collinear magnetization. As a model for our study we consider a diffusive junction with two ferromagnetic domains along the junction. The superconductor is assumed to be close to the critical temperature $T_c$, and the linearized Usadel equations predict a sinusoidal current-phase relation. We find analytically the critical current as a function of domain lengths and of the angle between the orientations of their magnetizations. As a function of those parameters, the junction may undergo transitions between 0 and $\pi$ phases. We find that the presence of domains reduces the range of junction lengths at which the $\pi$ phase is observed. For the junction with two domains of the same length, the $\pi$ phase totally disappears as soon as the misorientation angle exceeds $\pi/2$. We further comment on possible implication of our results for experimentally observable 0--$\pi$ transitions in SFS junctions.