The
Sanhaja (also "Sanhadja";
Arabic??????) were one of the largest
Berber tribal confederations of the
Maghreb, along with the
Zanata and
Masmuda.
The tribes of the Sanhaja settled at first in the northern Sahara. After the arrival of Islam they also spread out in the Sudan as far as the Senegal River and the Niger. From the 9th century Sanhaja tribes began to establish themselves in the Middle Atlas range, in the Rif Mountains and on the Atlantic coast of Morocco. A part of the Sanhaja settled in eastern Algeria (the Kutama), and played an important part in the rise of the Fatimids. The Sanhaja dynasties of the Zirids and Hammadids controlled Ifriqiya until the 12th century.
At the beginning of the 9th century a tribal kingdom of the Masufa and the Lamtuna formed in what is now Mauritania under Tilantan (d.826), which controlled the western Trans-Saharan trade route and fought the kingdoms of "Bilad as-Sudan" (not to be confused with modern Sudan). Although this empire fell apart at the beginning of the 10th century, the missionary and theologian Ibn Yasin managed to unite the tribes in the alliance of the Almoravids in the middle of the 11th century. This confederacy subsequently established Morocco, conquered western Algeria, and Andalusia in Spain, as well as the Ghana Empire.
With the invasion of the Maghreb by the Arab Banu Hilal tribe in the 11th century, the Sanhaja were gradually Arabized.