The building was established in a strategic location that connects several areas of Morocco. The Sebou River, which flows to the Atlantic Ocean, is a navigable river that flows through the Atlas Mountains and reaches inland areas such as the city of Fez. During the Middle Ages, holding this area was key for establishing a logistical, military and commercial advantage. As such, the kasbah was heavily contested by many states throughout history.[2] Its foundation dates back to an assembly building, likely built as a part of a harbor from the 10th century.[citation needed] In the 12th century, the Almohad Sultan Abd Al-Mu'min established a dock and attached a fort, using high quality woods from the neighboring forest. The town of Mehdya grew around it and remained small, but was used as a resource center for trade with Europe until the 16th century.
In 1515, the Portuguese captured the area and built a fort in a different location, and the occupation only lasted for some weeks.[2]
In the 17th century, after the site was briefly used as a hub for piracy, the local authority attempted to gain independence, only to be occupied by Spain.
The Spanish constructed the La Mamora fortress and San Felipe baston fort in 1614. It was the work of the royal engineer Cristobal de Rojas and Cristobal Lechuga artillery officer on top of the rocks that overlooks the mouth of the river. The citadel wall built as a part of the fort had become the basis of the current structure.
The area was recovered by the Alaouite Sultan Ismail Ibn Sharif in 1681, who gave the for, or kasbah, its current name.[2][1] Sultan Ismail carried out a full-scale reconstruction of the kasbah on the Spanish fort. He built an establishment for administration (Dar el-Makhzen), mosque, barracks, madrasa, funduq (traditional inn) and stables. The Sultan also restored the broken citadel walls and demolished the southern section to make way for defense towers and moats. He maintained the three towers on the northern side. He also constructed the monumental entrance gate on the shore, which was named as Bab al-Jadid. His detailed construction also extended to the interior, featuring four halls surrounding the sahn adorned by zellige. There is also a garden and hamam as well.[2][1] After closing the harbour to secure Kenitra in the late XVIII century the kasbah was abandoned.