During the French invasion of Algeria in 1830, the sultan of Morocco, Mawlāy ʿAbd al-Raḥmān (1822–59), briefly sent troops to occupy Tlemcen but withdrew them after French protests. The Algerian leader Abdelkader in 1844 took refuge from the French in Morocco. A Moroccan army was sent to the Algerian frontier; the French bombarded Tangier on August 4, 1844, and Essaouira (Mogador) on August 15. Meanwhile, on August 14, the Moroccan army had been totally defeated at Isly, near the frontier town of Oujda. The sultan then promised to intern or expel Abdelkader if he should again enter Moroccan territory. Two years later, when he was again driven into Morocco, the Algerian leader was attacked by Moroccan troops and was forced to surrender to the French.
Immediately after ʿAbd al-Raḥmān's death in 1859, a dispute with Spain over the boundaries of the Spanish enclave at Ceuta led Madrid to declare war. Spain captured Tétouan in the following year. Peace had to be bought with an indemnity of $20 million, the enlargement of Ceuta's frontiers, and the promise to cede to Spain another enclave—Ifni.
The new sultan, Sīdī Muḥammad, attempted with little success to modernize the Moroccan army. Upon his death in 1873, his son Mawlāy Hassan I struggled to preserve independence. Hassan I died in 1894, and his chamberlain, Bā Aḥmad (Aḥmad ibn Mūsā), ruled in the name of the young sultan ʿAbd al-ʿAzīz until 1901, when the latter began his direct rule.
ʿAbd al-ʿAzīz surrounded himself with European companions and adopted their customs, while scandalizing his own subjects, particularly the religious leaders. His attempt to introduce a modern system of land taxation resulted in complete confusion because of a lack of qualified officials. Popular discontent and tribal rebellion became even more common, while a pretender, Bū Ḥmāra (Abū Ḥamārah), established a rival court near Melilla. European powers seized the occasion to extend their own influence. In 1904 Britain gave France a free hand in Morocco in exchange for French noninterference with British plans in Egypt. Spanish agreement was secured by a French promise that northern Morocco should be treated as a sphere of Spanish influence. Italian interests were satisfied by France's decision not to hinder Italian designs on Libya. Once these various interests were settled, the Western powers met with Moroccan representatives at Algeciras, Spain, in 1906, to discuss the country's future.
The Algeciras Conference confirmed the integrity of the sultan's domains but sanctioned French and Spanish policing Moroccan ports and collecting the customs dues. In 1907–08 the sultan's brother, Mawlāy ʿAbd al-Ḥāfiẓ, led a rebellion against him from Marrakech, denouncing ʿAbd al-ʿAzīz for his collaboration with the Europeans. ʿAbd al-ʿAzīz subsequently fled to distant Tangier. ʿAbd al-Ḥāfiẓ then made an abortive attack on French troops, which had occupied Casablanca in 1907, before proceeding to Fès, where he was duly proclaimed sultan and recognized by the European powers (1909).
The new sultan proved unable to control the country. Disorder increased until, besieged by tribesmen in Fès, he was forced to ask the French to rescue him. When they had done so, he had no choice but to sign the Treaty of Fez (March 30, 1912), by which Morocco became a French protectorate. In return, the French guaranteed that the status of the sultan and his successors would be maintained. Provision was also made to meet the Spanish claim for a special position in the north of the country; Tangier, long the seat of the diplomatic missions, retained a separate administration.