Cairo stands where the Nile Valley opens into the Delta, a spot that has mattered since the pharaohs. Memphis, the Old Kingdom capital, lay a short way south-west, and the temple city of Heliopolis stood in today's Matariya and Ain Shams. Around 300 CE the emperor Diocletian built a Roman fortress called Babylon on the east bank. Its walls still enclose Coptic Cairo, where the city's oldest churches go back to around 700.
In 641 the Arab general Amr ibn al-As took Babylon and set up camp beside it. The garrison town became Fustat, "the tent", and his mosque there is the oldest in Africa, although rebuilt many times. Later governors kept building their own new quarters to the north-east. The Nilometer on Roda Island dates from 861, and Ahmad ibn Tulun's mosque, finished in 879, was the only part of his city spared when the Abbasids razed it in 905. It still keeps its original form.
Cairo itself was born in 969, when the Fatimid dynasty arrived from Tunisia and built a walled palace city north of Fustat. They named it al-Qahira, "the Victorious", and it gave the city its English name, though Cairenes still call it Masr, the same word as Egypt. For a century it stayed mostly a royal city. Between 1087 and 1092 the vizier Badr al-Gamali rebuilt the walls in stone, and his gates survive: Bab Zuweila on the south side, and on the north Bab al-Futuh with Bab al-Nasr.
Saladin took power in 1171 and in 1176 began the Citadel, which opened the palace city to ordinary Egyptians and foreign merchants. Then came the Mamluks, slave soldiers who ruled from 1250 to 1517. Under them the city reached close to half a million people by 1340, larger than any city west of China. The Black Death returned more than fifty times, yet the colossal madrasa of Sultan Hasan went up in those years. The most prominent buildings of medieval Cairo are Mamluk.
The sea route round the Cape, opened by Vasco da Gama, let spice traders bypass Cairo, and in 1517 the Ottomans made it a provincial capital. It still traded Yemeni coffee and Indian cloth, and al-Azhar became the leading school of the Islamic world. When Napoleon's army arrived in 1798 the city had fewer than 300,000 people. The French were gone by 1801, and in 1805 an Albanian officer, Muhammad Ali Pasha, became viceroy and founded the dynasty that ruled Egypt until 1952.
His grandson Isma'il, who ruled from 1863 to 1879, wanted Paris. He got a downtown of squares and boulevards west of the old city. He also got debts, which gave Britain its excuse to invade in 1882. Under British rule new garden suburbs went up, among them Heliopolis, built in 1906 for the Belgian Édouard Empain, and Zamalek on Gezira island. Mass protests in 1919 led to independence in 1922, but British troops stayed until 1956.
In 1952 rioters burned nearly 700 downtown shops, cinemas, casinos and hotels, and that year's revolution ended the monarchy. Nasser reshaped Tahrir Square and the Nile Corniche, and the city kept swelling: Greater Cairo grew from about 3 million people in 1947 to 16 million in 2006, and by 2009 most of them lived in informal neighbourhoods. In 2011 Tahrir Square filled with protesters from 25 January until Hosni Mubarak resigned on 11 February. In 2015 the government announced a new capital further east, beyond New Cairo.