The Treaty of Gandamak (Dari: معاهده گندمک, Pashto: د گندمک تړون) officially ended the first phase of the Second Anglo-Afghan War. Mohammad Yaqub Khan ceded various frontier areas as well as Afghanistan's control of its foreign affairs to Britain. It was signed on 26May 1879 by King Mohammad Yaqub Khan of Afghanistan and Sir Louis Cavagnari of British's Government of India at a British army camp near the village of Gandamak, about east of Kabul. The treaty was ratified by Lord Edward Robert Bulwer Lytton, Viceroy of India, on 30 May 1879. Most historical writings consider the Treaty of Gandamak as the prelude to the second phase of the Second Anglo-Afghan War, 1879–1880. As result of the British victory at the Battle of Kandahar in 1880 the treaty was reaffirmed and the British appointed Abdur Rahman as Emir. Background On 22 July 1878, a Russian delegation arrived in Kabul without the explicit invitation of Amir Sher Ali Khan. In early August 1878 to counteract the Russian initiative, the British informed the Amir that he must receive a special mission that included members of other European states "with all becoming honors." The mission was denied entry into Afghanistan at a military post commanded by Ali Masjid in the Khyber pass on 21 September. In retaliation, the British Government of India issued an ultimatum that by 20 November 1878 the Amir must apologize for the effrontery and provide a satisfactory explanation. Sher Ali's response of 19 November 1878, delayed by his own son and heir apparent's death on 17 August, did not reach the Viceroy until 30 November, and lacked an apology. On 21 November the British Raj declared war on Afghanistan, occupied the Korram valley and the Paywar pass, and moved its armed forces via the Khyber Pass and Quetta towards Jalalabad and Qandahar, respectively. Unable to offer effective military resistance, on 23 December 1878, the Amir left Kabul for Turkestan, intending to seek Russian aid for the defence of his domains. Sher Ali died on 21 February 1879 near Balkh and his son, Mohammad Yaqub Khan, declared himself Emir of Afghanistan. On 26 May 1879, after preliminary correspondence with Cavagnari prior to the British withdrawal from most occupied Afghan territories, Muhammad Yaqub's request for permission to visit the British military camp was accepted, and so he proceeded there to sign the Treaty of Gandamak, considered one of the most humiliating