Why didn’t the Ottoman Empire extend its dominion over Morocco?? What stopped the Ottoman armies at what is now roughly the Algerian border?

by theirishcampfire
Zooasaurus

I'll try to answer your question, sorry if it doesn't satisfy you. The Ottomans do interested and actually meddled in Moroccan affairs, and even at one point installed a client ruler in Morocco, effectively making it an Ottoman protectorate

First a bit of background. With the failures of the Wattasid Dynasty to protect Morocco from Portuguese attacks, a local dynasty named the Sa'adi Dynasty rose up in revolt. With support from local tribes and Jazuli sheikhs, the Sa'adis managed to capture Marrakesh and reconquered Agadir from the Portuguese, giving the Sa'adis significant legitimacy and power base to contest Wattasid authority. With the siege of Wattasid capital of Fez by the Sa'adi force, the former then sought help from the Ottomans entrenched in Algeria. The Ottomans then intervened in favour of the Wattasids, convincing the chief of Sa'adi dynasty Muhammad al-Mahdi to lift the siege of Fez. In return, the Sa'adis were to be recognized by the Ottomans as the "Shaykh al-Arab" or essentially a local ruler, while at the same time the Ottomans demanded the Sa'adis to preach hutbe and mint coins in the name of the Ottoman Sultan Süleyman I. The Sa'adis obviously weren't impressed, captured Fez anyway, and send a letter to Süleyman mocking him as sultan of fishermen and skiffs. This conflict eventually culminated into a fight for Tlemcen. The Sa'adis captured Tlemcen in 1549, in response the Ottomans in Algeria under Hasan Corso Pasha captured it from the Sa'adis a year later. The governor of Algeria Salih Pasha later marched into Fez defeating Al-Mahdi's forces under the pretext of supporting a Wattasid pretender in 1554, but then retreated fearing a popular revolt. After he reconsolidate his power in Fez, Al-Mahdi negotiated with Philip II to make an anti-Ottoman alliance. However the negotiations cut short because Al-Mahdi was mysteriously murdered, probably assassinated by Ottoman agents.

Al-Mahdi's successor Al-Ghalib didn't pursue an aggressive policy of his father, though in his earlier years he did kill his dynastic rivals. His surviving brothers Abdulmumin, Abdulmelik, and Ahmed al-Mansur of Civ V fame then took refuge to Tlemcen under Ottoman control. Süleyman, striving for stability in North Africa then reconciled with the Moroccans and established diplomatic relations, while also giving a strict order to Algerian governor to not meddle in Moroccan affairs anymore. However, his successor Selim II take a different approach. While he sent a letter to Al-Ghalib warranting peace, he secretly urged his ambassadors to negotiate with local Moroccan notables to revolt and install one of the princes in Tlemcen as client ruler.

Among the three princes in exile, Abdulmelik was the most active in supporting the Ottomans. He served under Uluj Ali in campaigns against Venice that eventually ended with the Battle of Lepanto, and later participated in the expedition to La Goletta where he distinguished himself as a proven military commander. After the assassination of Abdulmumin, he was granted a nishan (privilege grant) by Selim II claiming him as the legitimate ruler of Fez, Marrakesh, and Sous. With this, Abdulmelik theoretically could have an autonomous rule under Ottoman sultan in case he succeeded to overthrow his brother or nephew.

On January 1574 Al-Ghalib died and his son Muhammad succeeded him. Muhammad proclaimed himself as the enemy of Algeria, and so giving Abdulmelik a chance to overthrow him basing his claims on the nishan. Selim's successor Murad III granted his request and send troops to support Abdulmelik. And so backed by the Ottomans, Abdulmelik managed to raise 8,000 troops and 20 cannons, in addition of some 3,000 to 5,000 Ottoman soldiers. He departed from Algiers on December 1575 with the governor of Algeria Ramazan Pasha. The first battle took place in Ar-Rukn on March 1576, where Muhammad put up some 30,000 soldiers and 36 cannons. During the battle, Andalusi musketeers defected to Ottoman side, prompting Muhammad's army to retreat. With the defeat at Ar-Rukn, Fez was exposed and Abdulmelik conquered it. Muhammad was defeated a second time in the battle of Khaynuqa after which Abdulmelik conquered Marrakesh. Five months later, Muhammad accepted his defeat and fleed to Portugal and soon after the remaining regions of Morocco submitted to Abdulmelik.

After the conquest of Morocco, Abdulmelik was expected to pay back the expenses of the operation to the Ottomans, which he immediately did. Ramazan Pasha don't like the idea of staying in Morocco so he immediately headed back to Algiers after he left some auxiliary units. After Abdulmelik consolidated his rule (including discharging most Ottoman soldiers, leaving only some 400 elite cavalrymen as his personal guard) he ordered for the hutbe to read in the name of Murad III and golden coins minted in the latter's name, effectively making Morocco an Ottoman protectorate. Ramazan Pasha was also ordered by Murad to be careful with foreign invasion plans and to report anything about the newly 'conquered' country to make sure everything didn't turn out the way it did back when Salih Pasha marched into Fez on 1554. Murad also ordered for letters of conciliation and honorific robes to be sent to local Moroccan notables. Abdulmelik and his father in law Haci Murad also acted as unofficial representative of Murad III when dealing with Philip II' emissaries during the truce negotiation. Essentially though formally an Ottoman "protectorate" and Abdulmelik continued to revere the Ottoman Sultan, Morocco was to be governed as if it's independent, probably kinda like the Crimean Khanate.

Though things didn't last for so long. During the battle of three kings against the Portuguese in 1578, Abdulmelik was killed in fighting and was succeeded by his remaining brother Ahmad al-Mansur. Al-Mansur was hostile to Istanbul and was eager to establish permanent independence for his kingdom. The Ottomans were too busy with their negotiations with Spain and wars with the Persian Safavids to do anything significant. Even more so, the Moroccans struck a deal with the Spanish to assist Morocco in case of Ottoman invasion in exchange of Spanish control of Larache (which was finally occupied by the Spaniards in 1610, in exchange for the support for Al-Shaykh). After Al-Mansur's death, the Ottomans along with the Dutch supported Moulay Zidan, one of the claimants loyal to the Ottomans during the succession war. However Spanish support eventually made Al-Shaykh, the third contender victor of the struggle and the Sultan of Morocco whose son Abdullah went on to establish the Alawite Dynasty. The Ottomans faced a special problem with the Alawis. As their dynastic name suggests, this group claimed descent from Ali, the son-in-law of the Prophet, and boasted a pedigree that was more exalted than that claimed by the Ottoman dynasty. Many North African jurists viewed the Alawi claim to Sunni legitimacy to be stronger than that of the Ottoman dynasty. The practical result of the Alawi dynasty’s claim was that the sultans of Morocco could rally Berber and Arab tribes into coalitions, based on religious devotion to the Prophet Muhammad’s descendants, effectively resisting further Ottoman attempts to expand their political control into Morocco

Sources:

Ottoman Suzerainty over Morocco under Abdulmelik by Güneş Işıksel

Ahmad Al-Mansur, The Beginnings of Modern Morocco by Mercedes Garcia

The Ottoman Age of Exploration by Giancarlo Casale