It was in Kairouan that Islam gained its first foothold in the Maghreb. The original Arab settlement lasted only a few years before it was destroyed by a Berber rebellion. It was re-established in AD 694 by Hassan ibn Nooman and has been Islamic ever since.
The city's golden age began when it became the capital of the Aghlabid dynasty in AD 797. Although they preferred to rule from their palace at Raqqada, 9km south of Kairouan, it was the Aghlabids who endowed the city with its most important historic buildings, most notably the Great Mosque
Kairouan fell to the Fatimids in AD 909, and declined after the capital was moved to Mahdia. Its fortunes hit rock bottom when it was sacked in 1057 during the Hilalian invasions. It never regained its position of political pre-eminence but it retained its significance as a seat of Islamic scholarship and a holy city of Islam.