S-adenosylmethionine (SAM) riboswitches are widespread in bacteria, and up to five different SAM riboswitch families have been reported, highlighting the relevance of SAM regulation. On the basis of crystallographic and biochemical data, it has been postulated, but never demonstrated, that ligand recognition by SAM riboswitches involves key conformational changes in the RNA architecture. We show here that the aptamer follows a two-step hierarchical folding selectively induced by metal ions and ligand binding, each of them leading to the formation of one of the two helical stacks observed in the crystal structure. Moreover, we find that the anti-antiterminator P1 stem is rotated along its helical axis upon ligand binding, a mechanistic feature that could be common to other riboswitches. We also show that the nonconserved P4 helical domain is used as an auxiliary element to enhance the ligand-binding affinity. This work provides the first comprehensive characterization, to our knowledge, of a ligand-controlled riboswitch folding pathway.