{"document_url": "https://en.wikipedia.org/wiki/Morocco%E2%80%93Portugal_relations"}
Morocco–Portugal relations cover a period of several centuries largely historic, and to present not particularly substantial relations. Initial contacts started in the 8th century, when Muslim forces invaded most of the territory of the Iberian peninsula. After the Reconquista, Portugal would then expand into Africa, starting with the territory of Morocco, by invading cities and establishing fortified outposts along the Moroccan coast.

First Islamic expansion - Umayyad Caliphate (8th century)

Following the invasion of southern Iberian Peninsula by the Umayyad Caliphate with the Berber Commander Tariq ibn Ziyad in 711, during the 8th century Arab and Berber armies invaded the rest of Iberia, and even went beyond to Southern France, and as far as Poitiers and the Rhône valley, until the turning point of the Battle of Tours in 732. The Rio Douro eventually became the boundary between Christian and Muslim lands. The land between the Douro and Rio Minho was the Christian County of Portucale, which became the Kingdom of Portugal under Afonso Henriques in 1139.

Almoravid and Almohad invasions of the Iberian peninsula (11th and 12th centuries)
Iberian Peninsula would again be affected by the expansion of Muslim empires under the Almoravid and the Almohad dynasties in the 11th and 12th centuries.

The Portuguese managed to recapture Lisbon in the 1147 Siege of Lisbon.

Portuguese expansion in Morocco (1415–1515)
Portugal started to invade and occupy parts of coastal Morocco in 1415 with the conquest of Ceuta, which was besieged unsuccessfully three years later by the Moroccans. Then under Afonso V of Portugal, Portugal conquered Alcácer Ceguer (1458), Tangiers (won and lost several times between 1460 and 1464) and Arzila (1471). These achievements earned the king the nickname of the African.

Portugal and Spain had passed an agreement in 1496 in which they effectively established their zones on influence on the North African coast: Spain could only invade and occupy territory east of Peñon de Velez. This restriction would only end with the dynastic union of the Portuguese and Spanish crowns under Philip II after the 1578 Battle of Ksar El Kebir, when Spain began to take direct action in Morocco, as in the occupation of Larache.

Altogether, the Portuguese are documented to have seized six Moroccan cities and built six stand-alone fortresses on the Moroccan Atlantic coast, between the river Loukos in the north and the river of Sous in the south.

The six cities were: Ceuta (1415–1668), Alcácer-Ceguer (1458–1550), Tangier (1471–1661), Arzila (1471–1549), Safi (1488–1541) and Azamor (1513–1541).

Moroccan reconquest (1541–1769)
Of the six stand-alone fortresses, four only had a short duration: Graciosa (1489), São João da Mamora (1515), Castelo Real of Mogador (1506–10) and Aguz (1520–25). Two of them became permanent urban settlements: Santa Cruz do Cabo de Gué (Agadir, founded in 1505–06), and Mazagan founded in 1514–17.

The Portuguese had to abandon most of their settlements between 1541 and 1550 following the offensives of Mohammed ash-Sheikh, particularly the fall of Agadir in 1541 and the capture of Fez (1549). Nevertheless, they were able to keep a few bases: Ceuta (1415–1668), Tangier (1471–1661) and Mazagan (1502–1769).
The Battle of Ksar El Kebir in 1578 was a landslide loss, as the Portuguese king Sebastian of Portugal was killed in the encounter and his army eliminated by Moroccan forces in alliance with the Ottoman Empire.

Tangier was ceded to England in 1661 to encourage England to support Portugal in the Portuguese Restoration War, and Ceuta was handed over to Spain in 1668 through the Treaty of Lisbon, which recognized the House of Braganza as Portugal's new ruling dynasty and its rule over Portugal's remaining overseas colonies. These events essentially ended Portugal's direct involvement in Morocco. They abandoned Mazagan under the pressure from Mohammed ben Abdallah in 1769.

Five years later, in 1774, the Governments of Morocco and Portugal concluded a Peace and Friendship Agreement, one of the oldest bilateral agreements of both nations.

Resident diplomatic missions