information:filtering, linguistic:voice_change, structural:expansion, stylistic:tone, quality:correction
Territories in Africa (1885–1976)

By the end of the 17th century, only Melilla, Alhucemas, Peñón de Vélez de la Gomera (which had been taken again in 1564), and Ceuta (part of the Portuguese Empire since 1415, chose to retain their links to Spain once the Iberian Union ended. The formal allegiance of Ceuta to Spain was recognized by the Treaty of Lisbon in 1668), and Oran and Mers El Kébir remained Spanish territories in Africa. The latter cities were lost in 1708, reconquered in 1732 and sold by Charles IV in 1792.

In 1778, Fernando Pó (now Bioko), adjacent islets, and commercial rights to the mainland between the Niger and Ogooué rivers were ceded to Spain by the Portuguese in exchange for territory in South America (Treaty of El Pardo). In the 19th century, some Spanish explorers and missionaries would cross this zone, among them Manuel Iradier. In 1848, Spanish troops occupied the uninhabited Chafarinas Islands, anticipating a French move on the rocks located off the North-African coast.

In 1860, after the Tetuan War, Morocco paid Spain 100 million pesetas as war reparations and ceded Sidi Ifni to Spain as a part of the Treaty of Tangiers, on the basis of the old outpost of Santa Cruz de la Mar Pequeña, thought to be Sidi Ifni. The following decades of Franco-Spanish collaboration resulted in the establishment and extension of Spanish protectorates south of the city, and Spanish influence obtained international recognition in the Berlin Conference of 1884: Spain administered Sidi Ifni and Spanish Sahara jointly. Spain claimed a protectorate over the coast of Guinea from Cape Bojador to Cap Blanc, too, and even try to press a claim over the Adrar and Tiris regions in Mauritania. Río Muni became a protectorate in 1885 and a colony in 1900. Conflicting claims to the Guinea mainland were settled in 1900 by the Treaty of Paris, because of which Spain was left with a mere 26,000 km out of the 300,000 stretching east to the Ubangi River which they initially claimed.

Following a brief war in 1893, Morocco paid war reparations of 20 million pesetas and Spain expanded its influence south from Melilla. In 1912, Morocco was divided between the French and Spanish. The Riffians rebelled, led by Abdelkrim, a former officer for the Spanish administration. The Battle of Annual (1921) during the Rif War was a major military defeat suffered by the Spanish army against Moroccan insurgents. A leading Spanish politician emphatically declared: " We are at the most acute period of Spanish decadence ". After the disaster of Annual, Spain began using German chemical weapons against the Moroccans. In September 1925, the Alhucemas landing by the Spanish Army and Navy with a small collaboration of an allied French contingent put an end to the Rif War. It is considered the first successful amphibious landing in history supported by seaborne air power and tanks.

In 1923, Tangier was declared an international city under French, Spanish, British, and later Italian joint administration. In 1926, Bioko and Rio Muni were united as the colony of Spanish Guinea, a status that would last until 1959. In 1931, following the fall of the monarchy, the African colonies became part of the Second Spanish Republic. In 1934, during the government of Prime Minister Alejandro Lerroux, Spanish troops led by General Osvaldo Capaz landed in Sidi Ifni and carried out the occupation of the territory, ceded de jure by Morocco in 1860. Two years later, Francisco Franco, a general of the Army of Africa, rebelled against the republican government and started the Spanish Civil War (1936–39). During the Second World War the Vichy French presence in Tangier was overcome by that of Francoist Spain.

Spain lacked the wealth and the interest to develop an extensive economic infrastructure in its African colonies during the first half of the 20th century. However, through a paternalistic system, particularly on Bioko Island, Spain developed large cocoa plantations for which thousands of Nigerian workers were imported as laborers.

In 1956, when French Morocco became independent, Spain surrendered Spanish Morocco to the new nation, but retained control of Sidi Ifni, the Tarfaya region and Spanish Sahara. Moroccan Sultan (later King) Mohammed V was interested in these territories and unsuccessfully invaded Spanish Sahara in 1957, in the Ifni War, or in Spain, the Forgotten War (la Guerra Olvidada). In 1958, Spain ceded Tarfaya to Mohammed V and joined the previously separate districts of Saguia el-Hamra (in the north) and Río de Oro (in the south) to form the province of Spanish Sahara.

In 1959, the Spanish territory on the Gulf of Guinea was established with a status similar to the provinces of metropolitan Spain. As the Spanish Equatorial Region, it was ruled by a governor general exercising military and civilian powers. The first local elections were held in 1959, and the first Equatoguinean representatives were seated in the Spanish parliament. Under the Basic Law of December 1963, limited autonomy was authorized under a joint legislative body for the territory's two provinces. The name of the country was changed to Equatorial Guinea. In March 1968, under pressure from Equatoguinean nationalists and the United Nations, Spain announced that it would grant the country independence.

In 1969, under international pressure, Spain returned Sidi Ifni to Morocco. Spanish control of Spanish Sahara endured until the 1975 Green March prompted a withdrawal, under Moroccan military pressure. The future of this former Spanish colony remains uncertain.

The Canary Islands and Spanish cities in the African mainland are considered an equal part of Spain and the European Union but have a different tax system.

Morocco still claims Ceuta, Melilla, and plazas de soberanía even though they are internationally recognized as administrative divisions of Spain. Isla Perejil was occupied on 11 July 2002 by Moroccan Gendarmerie and troops, who were evicted by Spanish naval forces in a bloodless operation.