Fes is one of the four so-called "imperial cities" (the others are Marrakesh, Meknes and Rabat). Fes is separated into three parts, Fes-al-Baldi (the old, walled city), Fes-Djedid (new Fes, home of the Mellah), and the Ville Nouvelle (the French-created, newest section of Fes). The Medina of Fes-al-Baldi, the larger of the two medinas of Fes, is believed to be the largest contiguous carfree urban area in the world.
The city was founded by Idris I in 789. In 810 the Kairouyine mosque, one of the oldest and largest in Africa, was built by Idris II, and the associated university was founded in 859. The city was populated by Muslims from elsewhere in North Africa, the Middle East, Moriscos, as well as many Jews, who had their own quarter, or Mellah, in the city.
Fes became the center of the Alaouite Dynasty in 1649, and it was a major trading post of the Barbary Coast of North Africa. Until the 19th century it was the only source of Fez hats (also known as the tarboosh), before they began to be manufactured in France and Turkey; originally, the dye for the hats came from a berry that was grown outside the city, known as the Turkish kizziljiek or Greek akenia (Cornus mascula). Fes was also the end of a north-south gold trading route from Timbuktu.