Kairouan is said to be the fourth holiest city in Islam after Mecca, Medina and Jerusalem due to its founding in AD 671 by Oqba ibn Nafi, commander of the Arab army which was thrusting into North Africa. It was the Arabs first foothold in the region and they built the original settlement only for it to be destroyed by a Berber rebellion. The city was re-established in AD 694 by Hassan ibn Nooman and went through a golden period in the 8th and 9th centuries when it was the capital of the Aghlabid's who built the city's main attraction - the Great Mosque whose plan and architecture was used as a template for other such mosque's through-out the country. However, the Aghlabid's moved their capital to the coastal town of
Mahdia and the city declined and was eventually sacked by the Beni Hilal nomads in 1057. In the 14th century the town was rebuilt by the Hafsids, and in the early 18th century it was extended by the Husseinites who built the Medina walls and gates. Although
Tunis was now the uncontested political capital of Tunisia, Kairouan retained its religious importance for the Muslims of North Africa; and seven pilgrimages to Kairouan are regarded as equivalent to the prescribed pilgrimage to Mecca.