Let me offer sufficient conditions in both cases. They follow from the existence of the following two left proper model structures on the category of topological spaces, and the well-known gluing lemma holding in such categories:
Weak equivalences = weak homotopy equivalences, cofibrations = retracts of relative CW-complexes, fibrations = Serre fibrations [Quillen].
Weak equivalences = homotopy equivalences, cofibrations = closed immersion with the homotopy extension property, fibrations = Hurewicz fibrations [Strom].
In either case, it is enough to assume that $U\cap V$ contains a deformation retract $A\subset U\cap V$ such that $A\subset U$ or $A\subset V$ is a cofibration, and similarly fo $X'$.