The building was established in the strategic location which connects to several area of Morocco. Sebou River, which flows to the Atlantic ocean, was an easily navigable river and it flows through the Atlas Mountains and reaches the inland area such as the city of Fez. During the middle age, holding this area was a key for establishing the logistical, military and commercial advantage. As such, the kasbah was heavily contested by many states throughout the history. The foundation dates back to the assembly building which was likely built as a part of the harbor in the 10th century. In the 12th century, the Almohad Sultan Abd al-Mu'min established a dock and attached fort in the place, using the high quality woods from the neighboring forest. The rest of the city of Mehdya remained small but used as a resource center within the trade between Europe until the 16th century. In 1515, the Portuguese captured the area and the fort, but the occupation only lasted for 47 years. In the 17th century, after the place was briefly used as a center for piracy, the local authority had attempted to gain independence, only to be occupied by the Spain. The Spanish constructed a fort on top of the stone which overlooks the mouth of the river. The citadel wall built as a part of the castle had become the basis of the current structure. The area was recovered by the Alaouite Sultan Ismail Ibn Sharif in 1681, who gave the castle its current name. Sultan Ismail carried out a full scale construction of the kasbah in the ruins of the Spanish fort. He built an establishment for administration, mosque, barracks, madrasa, funduq and stables. The Sultan also restored the broken citadel walls, and demolished the southern part of the wall for the construction of 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, where exist four halls surrounding the sahn adorned by zellige. There exist a garden and hamam as well. It is also known as the location of the Battle of Port Lyautey during the World War II.