The walls have since been expanded several times by the addition of the Kasbah to the south at the end of the 12th century and by a later extension to encompass the northern neighbourhood around the Zawiya of Sidi Bel Abbes.
[1][2] At first, the city's only major fortification was the Ksar al-Hajjar ("Palace/Fortress of Stone"), a royal citadel built by Abu Bakr to protect the treasury.
[1]: 57 It was the first monumental structure built by the Almoravids and marked their definitive transition from a nomadic Saharan people to an empire with a fixed base.
[1] It was only in 1126 that Ali ibn Yusuf, a later Almoravid amir, decided to surround the city with a full circuit of walls, which were completed in January or February 1127.
[1][2] The Almohad caliph Ya'qub al-Mansur (ruled 1184–1199), however, embarked on an ambitious construction project to create a vast new royal district, the Kasbah, attached to the south side of the city.
[1][2][4] The current western and southern outline of the kasbah today, including its walls, most likely still date back essentially to the Almohad construction.
Sultan Moulay Abdallah al-Ghalib also transferred the Jewish population of the city to a new Mellah district on the east side of the Royal Palace, expanding the eastern outline of the kasbah in the process.
[1][8] Sultan Ahmad al-Mansur also renovated and replanted the Agdal Gardens, thus maintaining this large walled enclosure to the south of the city.
[8] The Saadians, and their successors the Alaouites, also sponsored the construction and expansion of the zawiya and mosque complex around the mausoleum of Sidi Bel Abbes, which was located just outside the northern gate of the city, Bab Taghzout.
Sidi Bel Abbes is often considered the patron saint of Marrakesh[9] and his zawiya attracted more and more settlers to the area until a flourishing neighbourhood developed here outside the walls of the city.
Sultan Muhammad ibn Abdallah is also responsible for much of the construction and redevelopment of the royal palace (Dar al-Makhzen) following years of neglect, giving it more or less its present-day form.
Many of them are of minor architectural interest, though Bab Ahmar (the easternmost gate) has a unique design that includes an elevated platform for light artillery.
[1] Lastly, following its destruction by the Rehamna in 1862, the western wall of the Agdal Gardens was rebuilt by Sultan Muhammad ibn Abd al-Rahman.
[1][11] The walls of Marrakesh, like those of Fes and most historic cities in Morocco, were built in rammed earth, an ancient building technique found across the Near East, Africa, and beyond.
[13][16] This process of initial wooden scaffolding often leaves traces in the form of multiple rows of little holes visible across the face of the walls.
[17] In many cases walls were covered with a coating of lime, stucco, or other material to give them a smooth surface and to better protect the main structure.
[15] This type of construction required consistent maintenance and upkeep, as the materials are relatively permeable and are more easily eroded by rain over time; in parts of Morocco, (especially near the Sahara) kasbahs and other structures made with a less durable composition (typically lacking lime) can begin to crumble apart in less than a couple of decades after they've been abandoned.
[15][18] As such, old structures of this type remain intact only insofar as they are continuously restored; some stretches of wall today appear brand new due to regular maintenance, while others are crumbling.
[2] Nowadays, simple archways have been opened alongside many of them to allow for easier passage in and out of the medina, along with other gaps in the city walls created to accommodate new roads.
[1][2] The word Robb or Rubb refers to a type of cooked wine whose vineyards were cultivated along the Neffis River and thus imported and regulated through this gate.
[2][3] A water basin measuring approximately 70 by 40 metres once existed outside this gate, in an area now covered by a cemetery, and was used for swimming practice.
[3] Like Bab al-Makhzen to the south, the gate is flanked by octagonal towers and originally had a simple bent passage (turning 90 degrees to the north), but has since been modified.
[3] The gate is also of Almoravid construction, but unlike many others it has not been subject to major modifications (at least in its floor plan) and retains its original sophisticated bent entrance design.
These include: At least four more gates, possibly five (depending on the status of Bab Yintan) formerly existed along the perimeter of the medina's city walls but have since disappeared or been rendered obsolete.
[1][2] The gate's name, Taghzout, is of unclear historical origin but is a common Berber toponym with a few different meanings; it may have referred to a nearby village or to the valley of the Tensift River.
[3] The gate resembled the nearby Bab el-Khemis in its original configuration: it was flanked by two square bastions and had a bent entrance by which one entered from the north and exited to the west.
[2] Today, however, the gate has lost its bastions and its bent passage and all that remains is a large archway over the main street to the south of the Zawiya of Sidi Bel Abbes.
[3] A large open-air prayer area, called a musalla or msalla, was located just outside the gate during the Almoravid and Almohad era (though it may have been moved during the construction of the Kasbah).
The gate was originally flanked by two bastion towers and the passage inside was a bent entrance (meaning it turned 90 degrees before exiting) passing through a large vaulted vestibule.
[1]: 501 Under Muhammad ibn Abdallah (governor of Marrakesh after 1746, sultan 1757–1790), the Royal Palace (Dar al-Makhzen) in the Kasbah was renovated and expanded.