The two world powers – the Spanish and Ottoman empires – fought over control of North Africa, including Ifriqiya, in the 16th century. In the 15th century, the Ottomans had conquered Constantinople and the Balkans and early in the 16th century, they took Syria and Egypt. They then looked towards the eastern Mediterranean for possible further expansion of their empire. At the beginning of the 16th century, Spain had already occupied several strategic places on the Mediterranean, including the cities of Tripoli, Bougie (Béjaïa), Mers al-Kebir, and Oran, and it had made treaties with other cities, such as Algiers and Tunis. Located close to Tunis, Spain would eventually also take over the port of La Goulette. Spain’s occupation of several strategic positions in the Mediterranean and greater Africa encouraged the Ottomans to expand to North Africa to challenge Spanish supremacy in the region. The Ottomans posed a greater threat than Spain to the rulers of the Hafsid dynasty, despite the fact that many Arab Muslims and religious scholars preferred Islamic rule to that by a Christian power. Ottoman rule in North Africa was launched by the brothers Oruç and Hızır (Barbarossa), who came to Tunis as commanders of the Ottoman corsairs, privateers, and pirates operating from North Africa. By 1510, the brothers had prospered so much that they decided to set up their own independent base, and they moved to La Goulette and Djerba. Soon after their arrival, they began to make deals with the Hafsids to share the profits of the corsairs.
At the beginning of the 16th century, the two brothers moved to what is now Algeria with the intention of conquering Algiers. At that time, the Spanish ruled Algeria, and, although they did not control the city of Algiers militarily, they held the port from occupied Penon Island. Soon after entering Algeria, the older brother, Oruç, was killed in an attempt to conquer Tlemcen. Barbarossa then conquered Algiers. In the following years, Barbarossa became very popular, having won several battles at sea, for which the Ottoman sultan granted him the title of Pasha of the navy. Barbarossa took Tunis in 1534 and was increasingly busied with developments outside North Africa. Ottoman rule over Ifriqiya would, however, be strengthened again in 1556, when Dragut or Turgut, a corsair who ruled Tripoli, attacked Tunisia and in 1558 entered its holiest city, Kairouan. Attacks from the west soon followed, and in 1569 Uluç Ali, Barbarossa’s successor in Algiers, took Tunis, the capital of the Hafsid dynasty.
While the 1569 capture of Tunis was challenged by Spain – the Spaniards took the city in 1573 – Uluç Ali in the following years began to assert himself over Ifriqiya. To demonstrate his supremacy, he imprisoned the last ruler of the Hafsid dynasty. In 1581, the Ottomans entered into a truce with Spain, which eased relations between the powers and divided their rule in North Africa, with the Ottomans ruling Ifriqiya until the 19th century.
During the following years, the Ottomans were the world’s leading Islamic nation; their sultan held the title of caliph. Ottoman rule strongly influenced Tunisian cultural and legal history. For example, Ottoman imperial law was based not only on Islamic jurisprudence (fiqh) but also on jurisprudence stemming from the Roman-Byzantine era and the customs and traditions of the Ottomans and Mongol empires. The Ottomans favored the Hanafi school over the Tunisian Maliki school, but some Maliki jurists were still allowed to pass judgments.
The Ottoman Turkish language increasingly influenced the business elite and writers in Tunisia and accentuated linguistic divisions in Tunisia, by elevating Arabic over Berber, a development that can be clearly seen in modern-day Tunisia.
The Ottomans were initially welcomed in urban areas, as they were perceived as promoting the stability of the cities, but things were very different in rural areas, where the Turkish elite was considered dominating and aloof. The Turks never managed efficiently to control and regulate the economy of the countryside. Stability under the Ottomans was constantly threatened from the outside as well as from within Turkish circles. A pasha became governor of Ifriqiya province in 1587, and directly under him was the so-called bey of Tunisia.
In 1591, turmoil emerged in the Turkish army, and the dey – the title of a new military commander – became the ruler of Tunis. In the beginning, his authority was limited to the capital and few other cities. Under Uthman Dey and Yusuf Dey, his son-in-law, Tunisia enjoyed relative prosperity and stability. Construction, including mosques and fortresses, and other public projects were financed by the treasury. Various interest groups emerged that would eventually challenge Yusuf Dey’s authority.
These groups eventually supported the Turkish beylik (the territory under the bey), then held by the Muradids. The founder of this bey dynasty, Murad, a Corsican, gained a great reputation, for his success, for example, in suppressing rebellious tribes. This led the Ottoman sultan to grant him the title of Pasha, which bestowed on Murad legitimacy and dignity and contributed to the gradual decrease, in the 1670s, of the authority of the dey. The Muradid beys, however, were torn by internal divisions that led Algerian forces to enter Tunisia, where they remained for several years. The shift in power from the deys to the beys was accompanied by a changing economic policy, due in part to the decrease in corsair income resulting from European pressure. Agricultural commerce increased, as the urban areas became increasingly integrated, but the Tunisian elite of ruling authorities and businessmen gained more from trade than did other social groups, sharpening social divisions.
Al-Husayn I ibn Ali, an officer in the Ottoman cavalry, took power in 1705, his military has taken part in beating back the Algerian invasion of Tunisia. Al-Husayn ibn Ali’s close connections and solidarity with Tunisian tribes and religious authorities helped him stand up against the dey, who was also seeking authority over the country. Al-Husayn ibn Ali strove to gain popularity by appointing Maliki religious scholars and jurists instead of those of the Hanafi school, who had generally been favored during Ottoman rule, and by combining force and cooperation to secure the loyalty of Berber tribes. He also launched public-works projects, such as the building of schools and mosques, in order to increase his legitimacy. The relative stability of his regime was, however, challenged in 1735, when his nephew Ali and son Muhammad engaged in a dispute regarding the succession to al-Husayn ibn Ali. This resulted in a five-year civil war, which ended with the victory of Ali. This victory was, however, reversed sixteen years later, and Muhammad became the final successor of the Husaynid beys.