The part of Spain which continued to resist French occupation remained loyal to Ferdinand VII and allied with Britain and Portugal to expel Napoleon's armies from Spain. The war would be much of a back and forth, till from 1812-1813 Allied victories at Salamanca and Vitoria meant the defeat of the Bonapartist régime and the expulsion of Napoleon's troops. The Treaty of Valençay recognized Ferdinand VII as the legitimate King of Spain.