LOFT. Unlike SWEEP, it lets you define the specific orientation [and even different shapes/sizes/etc.] at both ends.
Draw the end shapes that are rectangles in your example [red], and the path [yellow] -- it seems to be important that the path touches the rectangles. LOFT command, pick the rectangles & Enter to end, and a funny temporary thing will appear, but choose the Path option and pick the yellow.
Vary the twistiness with additional intermediate rectangles -- here the bottom is the one from above, the middle is the one from including the midway rectangle in the top un-Lofted pieces:
[You must select the rectangles in the order you want them along the result.]
Multiple shapes:
Kent Cooper, AIA