and the annihilation of Elphinstone's army, and the punitive expedition of The battle of Kabul that led to its sacking, the British gave up on their attempts to try and subjugate Afghanistan, which allowed Dost Mohammad Khan to return as ruler. Dost Mohammad Khan would spend most of his reign consolidating the parts of Afghanistan that were lost in the Afghan civil war which raged from 1793–1863. Dost Mohammad Khan would launch numerous campaigns after returning to rule in 1842, ruling only from Kabul, Ghazni, and other cities when he had returned. Dost Mohammad united most of the Afghan realm in his reign, securing the last major state, Herat, in the Herat campaign of 1862–1863. Dost Mohammad died on 9 June 1863, a few weeks after his campaign to capture Herat. Dost Mohammad's successors would fight for the throne of Afghanistan, between Sher Ali Khan, Mohammad Afzal Khan, and Mohammad Azam Khan in the Afghan Civil War (1863–1869). Sher Ali would win this civil war and would go on to rule the realm until In 1878, the British had returned in the Second Anglo-Afghan War which was fought over perceived Russian influence in the region, Abdur Rahman Khan replaced Ayub Khan who had succeeded Sher Ali Khan after his death in 1879. Britain would gain control of Afghanistan's foreign relations as part of the Treaty of Gandamak of 1879, making it an official British Protected State. In 1893, Amir Abdur Rahman signed an agreement in which the ethnic Pashtun and Baloch territories were divided by the Durand Line, which forms the modern-day border between Pakistan and Afghanistan. Shia-dominated Hazarajat and pagan Kafiristan remained politically independent until being conquered by Abdur Rahman Khan in 1891–1896. He was known as the "Iron Amir" for his features and his ruthless methods against tribes. The Iron Amir viewed railway and telegraph lines coming from the Russian and British as "trojan horses" and therefore prevented railway development in Afghanistan. He died in 1901, succeeded by his son, Habibullah Khan. During the First World War, when Afghanistan was neutral, Habibullah Khan was met by officials of the Central Powers in the Niedermayer–Hentig Expedition, to declare full independence from the United Kingdom, join them and attack British India, as part of the Hindu–German Conspiracy. Their efforts to bring Afghanistan into the Central Powers failed, but it caused discontent among the population for keeping neutrality against the British.