Since the foundation of the first Moroccan state by Idris I in 788 AD, the country has been ruled by a series of independent dynasties, reaching its zenith under Almoravid and Almohad rule, when it spanned parts of Iberia and northwestern Africa. The Marinid and Saadi dynasties resisted foreign domination into the 17th century, allowing Morocco to remain the only northwest African country to avoid Ottoman occupation. The Alaouite dynasty, which rules to this day, seized power in 1631. The country's strategic location near the mouth of the Mediterranean attracted the interest of Europe, and in 1912, Morocco was divided into French and Spanish protectorates, with an international zone in Tangier. It regained its independence in 1956, and has since remained comparatively stable and prosperous by regional standards, with the fifth largest economy in Africa.
The full Arabic name al-Mamlakah al-Maghribiyyah (المملكة المغربية) translates to "Kingdom of the West"; although "the West" in Arabic is الغربAl-Gharb. The name also can refer to evening. For historical references, medieval Arab historians and geographers sometimes referred to Morocco as al-Maghrib al-Aqṣá (المغرب الأقصى, meaning "The Farthest West") to distinguish it from neighbouring historical regions called al-Maghrib al-Awsaṭ (المغرب الأوسط, meaning "The Middle West") and al-Maghrib al-Adná (المغرب الأدنى, meaning "The Nearest West").
Morocco's English name is based on Marrakesh, its capital under the Almoravid dynasty and Almohad Caliphate. The origin of the name Marrakesh is disputed, but is most likely from the Berber words amur (n) akush (ⴰⵎⵓⵔ ⵏ ⴰⴽⵓⵛ) or "Land of God". The modern Berber name for Marrakesh is Mṛṛakc (in the Berber Latin script). In Turkish, Morocco is known as Fas, a name derived from its ancient capital of Fes. However, this was not the case in other parts of the Islamic world: until the middle of the 20th century, the common name of Morocco in Egyptian and Middle Eastern Arabic literature was Marrakesh (مراكش); this name is still used in some languages such as Persian, Urdu and Punjabi. The English name Morocco is an anglicisation of the Spanish "Marruecos", from which also derives the Tuscan "Morrocco", the origin of the Italian "Marocco".