Having colonies was not always easy and in Morocco it was no piece of cake. From 1840 until 1850, the Spanish empire had the cities of Ceuta and Melilla in the north African country under constant and tireless assaults from Moroccan tribes and troops.
When a consul agent was murdered in 1844, the Spanish President, General Narváez had had enough and presented a formal complaint to the sultan Muley Soleiman. After a number of long and difficult discussions between Spain and Morocco's governments (with England acting as the mediator between the two) they finally reached an agreement and two covenants were signed (Tangier, 1844 and Laroche, 1845).
Despite these treaties, the attacks didn't stop. Ceuta and Melilla kept receiving continuous assails. Spain put up a fight but it was impossible to advance into Moroccan territory to go pursue the attackers. Finally the Spanish government, fed up, took over the Chafarinas Islands, near Morocco without warning in an invasion campaign in 1848, beating the French by six hours, who planned to plant their flag on the Chafarinas themselves, so to speak.
The Chafarinas Islands is a three island archipelago located 4 km away from north African coast (the nearest continental town is Ras el Ma), with a distance of 27 nautical from the east of Melilla. At the time they were considered "res nulius", no man's land, as they were uninhabited and only used as a station by pirates, though the sultan considered them as part of Moroccan territory. After general Narváez ordered the conquest, the Spanish troops left from Melilla and Malaga and disembarked on the islands on January 6, 1848.
Very shortly after the Chafarinas Islands occupation began, Spain decided to defend the borders agreed in their North African city of Ceuta and began the construction of several forts. In 1859 the Spanish detachment that guarded the construction of one of the buildings was attacked by a Rif squad. They destroyed some parts of the fortifications and they took down and damaged the Spanish shield, a great insult in the eyes of Spain.
It should be noted that by then Spain had lost most of it's empire overseas and the government was looking for ways to expand commercially and gain influence and authority in the eyes of Europe. On the other hand the Moroccan tribes were not about to relent so easily, in spite of any treaties their Sultan might have signed, they did not want to let go of Ceuta and Melilla.
The Spanish President of the time, Leopoldo O'Donnell didn't want to miss out on the opportunity of placing Spain among the first world powers and according to him, a great military victory was the way to do it, something that would put Morocco and Spain at war.
Using a line of diplomacy he demanded Morocco to punish the attackers to make an example of them and presented an ultimatum. He would replace the border shields and force the sultan's troops to salute them – any guilty assailants should be punished in the Spanish garrison at Ceuta. Apparently the Muley Mohammed accepted the terms of agreement, but he died shortly after and his son never kept his promise.
The Spanish government moved quickly to get all the diplomatic support from other European countries, persuading them with such words as sullied honour and lack of safety in frontiers and borders.
After the unanimous approval of the parliament, the popular and higher Spanish classes supported the war declaration on the north African nation on October 22, 1849. France and the UK also expressed their consent, although England was reluctant because of the possibility that their control of the Strait of Gibraltar would be threatened. In exchange Spain had to agree that it wouldn't remain in Tetuan or Tangier, so they wouldn't be tempted to occupy them permanently and to stay away from the strait.
In Catalonia and the Basque Country thousands of young men flocked en masse to the armies' voluntary recruitment centres in a surge of patriotic feeling that had not been experienced in the country for half a century. Malaga and Algeciras (Cadiz) also recruited large numbers, over 35,000 men, with them a fleet of 41 ships and 75 cannons. This was not the first war between Morocco and Spain, but the population seemed to consider it important.
The Spanish forces were divided into three bodies and by mid December, the bulk of them had arrived in Ceuta. General O'Donnell put himself in command of the force that arrived on the 21st. The Spanish army's aim was to take the north African cities of Tetuan and Tangier. On January 1, 1860, the Battle of Castillejos took place and earned Spain an initial victory
After this they moved on, but Spain's advances met Moroccan forces on the first days of February and the the Battle of Tetuan took place, they finally took the city on the 6th of February. The troops moved to their next objective: Tangier.
The Spanish army received important reinforcements from the Basque country, the fresh troops troops arrived and the Wad-Ras battle took place on March 23. Spain claimed the victory and the Moroccan commander Muley Abbás surrendered. 32 days later Morocco and Spain singed the Treaty of Wad Ras (Tetuan).
Morocco was declared loser and singularly culpable for the war. Spain obtained expansion of their Ceuta and Melilla territory in perpetuity, the end of assaults on both cities, sovereignty over the Chafarinas Islands and a 100 million pesetas compensation from Morocco.