information:extraction, quality:correction
Foundation and the Idrisids

The city was first founded in 789 as Madinat Fas on the southeast bank of the Jawhar River (now known as the Fez River) by Idris I, founder of the Idrisid dynasty. Idris I was an Hasanid Sharif from Arabia who was forced to flee the Hejaz after a failed revolt against the Abbasid Caliphate, eventually moving to northern Morocco and conquereing much of the area. His son, Idris II, built a settlement called al-ʿĀliyá on the opposing river bank in 809 and moved his capital here from Walili (Volubilis). The early population was composed mostly of Berbers, along with hundreds of Arab warriors from Kairouan who made up Idris II's entourage.

Arab immigration to Fez increased afterwards. Andalusi families of mixed Arab and Iberian descent, who were expelled from Córdoba after a rebellion in 817–818 against al-Hakam I, were one major component of the immigrant population. These families mainly settled in Madinat Fas. These two waves of immigrants gave the city its Arabic character and would subsequently give their name to the districts of 'Adwat Al-Andalus and 'Adwat al-Qarawiyyin. The city also had a prominent Jewish community, probably consisting of Zenata Berbers who had previously converted to Judaism, as well as a small remaining Christian population for a time. The Jews were especially concentrated in a northeastern district of al-ʿĀliyá, known as Funduq el-Yihoudi (near the present-day Bab Guissa gate).

Following the death of Idris II in 828 the region was divided among his sons. The eldest, Muhammad, received Fez, but some of his brothers attempted to break away from his leadership, resulting in an internecine conflict. Although the Idrisid realm was eventually reunified and enjoyed a period of peace under Ali ibn Muhammad and Yahya ibn Muhammad, it fell into decline again in the late 9th century.

In the 10th century, the city was contested by the Umayyad Caliphate of Córdoba and the Fatimid Caliphate of Ifriqiya (Tunisia), who ruled the city through a host of Zenata clients. The Fatimids took the city in 927 and expelled the Idrisids definitively, after which their Miknasa (one of the Zenata tribes) were installed there. The city, along with much of northern Morocco, continued to change hands between the proxies of Córdoba and the proxies of the Fatimids for many decades. Following another successful invasion by Buluggin ibn Ziri in 979 and a brief period of Fatimid control, the forces of Al-Mansur of Cordoba managed to retake the region again, expelling the Fatimids permanently. From 980 (or from 986), Fez was ruled by a Zenata dynasty from the Maghrawa tribe, who were allies of the Caliphate of Córdoba. They maintained this control even after the Caliphate's collapse in the early 11th century and until the arrival of the Almoravids.

Fez continued to grow under Zenata control, even though conflicts between its two settlements, Madinat Fas and al-ʿĀliyá, flared up during periods of political rivalry. Ziri ibn Atiyya, the first ruler of the new dynasty, had a troubled reign. However, Ibn Atiyya's descendant Dunas ibn Hamama, ruling between 1037 and 1049, was responsible for improving the city's infrastructure. He developed much of Fez's water supply system, which has largely survived up to the present day. Other structures built in his time included hammams (bathhouses), mosques, and the first bridges over the Oued Bou Khrareb (mostly rebuilt in later eras). The two cities became increasingly integrated: the open space between the two was filled up by new houses and up to six bridges across the river allowed for easier passage between them. A decade after Dunas, between 1059 and 1061, the two cities were ruled separately by two brothers who were rival Zenata emirs that fought with each other: al-ʿĀliyá was controlled by an emir named Al-Gisa and Madinat Fas was controlled by Al-Fetouh. Both brothers fortified their respective shores, and their names have been preserved in two of the city's gates to this day: Bab Guissa (or Bab Gisa) in the north and Bab Ftouh in the south.