[2] Following the Reconquista of the Iberian Peninsula, Spain looked south to the North African coast, capturing Melilla from the Sultanate of Fez in 1497, with Portugal's blessing.
[3][4] Repeated Moroccan attempts to gain control of the two exclaves by force during the 18th-19th centuries failed, culminating in the Hispano–Moroccan War of 1859–60, which resulted in a Spanish victory.
[3] Melilla's border was supposed to be fixed by 'the range of a piece of cannon', however this proved impractical, and a further treaty was therefore signed on 30 October 1861 and confirmed on 26 June 1862 which outlined the modern boundary line.
[10] In response to an increase in the number of migrants attempts to breach the fence, both were significantly fortified in 2005, creating a doubled-fence system replete with barbed wire and surveillance equipment.
It was widely speculated that Morocco permitted the sudden influx to punish Spain for allowing Polisario Front leader Brahim Ghali to be treated at a Spanish hospital.
[14][16] Morocco and Spain also share a maritime border in the Canary Islands area and along the Strait of Gibraltar and the Alboran Sea.