In the Nineteenth Century, the Russian Empire and Great Britain vied for control of Central Asia. For the Russians, it gave them a base from which to attack British India and potentially secure ports on the Indian Ocean and new trade routes to their empire. For the British, the goal was to prevent any incursions into the British Raj, which meant pushing their defence lines further North. The ultimate country to resist the influence of both powers was the landlocked Afghan Emirate.
The British first believed they could convince the ruler of Afghanistan to accept their protection, in a way many large Indian States had -- British India was a patchwork of provinces under direct rules and princely states under protection.
A first attempt in 1838 resulted in a complete defeat for Britain, the difficult terrain and skills of Afghans warriors proving too much for the most powerful army in the World. While both powers consolidated their control of neighbouring regions - the British in Punjab in the Southwest, the Russians in Bukhara in the North, it all came down to a second attempt by the British to take the Emirate under their control. The Second Anglo-Afghan War took place from 1880 and saw a British victory, but Britain understood they had better maintain an independent friendly regime that would act like a buffer state against Russian interests, rather than a submission to protectorate status. As a result, Afghanistan has been an independent nation continuously since 1747. While still subject to major tribal warfare, the country has not been under foreign rule despite successive occupation attempts by Great Britain, the Soviet Union and the USA.