Ayub Khan, who had been serving as governor of Herat, rose in revolt, defeated a British detachment at the Battle of Maiwand in July 1880 and besieged Kandahar. Roberts then led the main British force from Kabul and decisively defeated Ayub Khan in September at the Battle of Kandahar, bringing his rebellion to an end. Abdur Rahman had confirmed the Treaty of Gandamak, leaving the British in control of the territories ceded by Yaqub Khan and ensuring British control of Afghanistan's foreign policy in exchange for protection and a subsidy.