Treaty of Badajoz (1801)

The Treaty of Badajoz was signed by Spain and Portugal on 6 June 1801. Portugal ceded the border town of Olivença to Spain and closed its ports to British military and commercial shipping.
On the same day, Portugal signed a separate Treaty of Badajoz with France, which Napoleon, then First Consul of France, refused to sign. An amended version was agreed in September 1801, which is known as the Treaty of Madrid; France received large parts of Portuguese South America in what is now Brazil plus a payment of 20 million francs.
Background.
For much of the 18th century, Spain and France were allies but after the execution of Louis XVI in 1793, it joined the War of the First Coalition against the French First Republic. After being defeated in the War of the Pyrenees, Spain left the Coalition and made peace with France by the 1795 Peace of Basel.
Under Charles IV, government was controlled by Chief Minister Manuel Godoy, as the King spent most of his time hunting. Driven by Godoy, Spain agreed an alliance with France in the August 1796 Second Treaty of San Ildefonso and declared war on Britain, then engaged in the 1798-1802 War of the Second Coalition. 
Portugal had also joined the First Coalition but unlike Spain did not make peace with France. It was in a difficult position; Napoleon demanded they support his economic blockade by closing their ports to British shipping but their economy was dependent on trade with Brazil. These links could easily be cut by a hostile Royal Navy while Britain was also the main market for Portuguese agricultural products. Economic self-interest meant Portugal inclined towards Britain but needed support; between 1791 and 1801, the British government supplied money, supplies and a force of 6,000 soldiers under General Charles Stuart.
Stuart captured the Spanish island of Menorca in 1798, previously occupied by Britain from 1708 to 1782 and whose recovery was the major achievement of Spain's participation in the 1778-1783 Anglo-French War. The loss undermined Godoy, who had been removed as Chief Minister in 1797 and promoted to Captain-General. 
British troops were withdrawn from Portugal in early 1801; Godoy returned as Chief Minister and in May, Spain invaded Portugal in the War of the Oranges. The main focus was the siege of the Portuguese town Elvas but neither side pursued the war with much enthusiasm. When a French army corps entered North-Eastern Spain to 'support' their Spanish allies, the two quickly came to terms. 
There was also limited action in South America where Spain and Portugal had been arguing for 300 years over the delineation of borders in the Río de la Plata region. These had been fixed by the First Treaty of San Ildefonso in 1777 and the 1778 Treaty of El Pardo but war in Europe gave both sides an excuse to improve their positions. A Spanish attempt to seize Mato Grosso in modern Brazil was repulsed while the Portuguese captured the Misiones Orientales, allocated to Spain in 1778. Although not referenced in the Treaty of Badajoz, this territory has been part of Brazil since independence from Portugal in 1822.
Provisions.
On 6 June, Spain and Portugal signed the Treaty of Badajoz, the main provisions being the transfer of Olivença and Almeida to Spain and the banning of British ships from Portuguese ports. Possession of Olivença had been disputed since the Treaty of Alcañices in 1297; Portugal eventually recovered Almeida but Olivença remains under Spanish control.
On the same day, Portugal signed a second Treaty of Badajoz with France, represented by Napoleon's younger brother Lucien Bonaparte, granting France substantial territorial gains in South America. The modern border between French Guiana and Brazil is the Oyapock River, which was agreed in 1713; the proposed Treaty moved it south to the Araguari or Amapá River, taking in large parts of Northern Brazil. Portugal also agreed to close its ports to British shipping, pay an indemnity of 2 million francs and allow the import of French woollen goods.
However, Napoleon refused to ratify the Treaty, claiming Lucien Bonaparte who signed it and his Foreign Minister Talleyrand who agreed the terms had both been bribed by the Portuguese.
Aftermath.
To minimise the impact of the ban on using Portuguese ports, in July a British force occupied the island of Madeira; this was used by the Royal Navy to assemble convoys of merchant shipping that were then escorted into British ports.
However, Britain and France were already negotiating the Treaty of Amiens which ended the War of the Second Coalition in March 1802 and re-opened Portuguese ports. Spain also suspended the 1796-1808 Anglo-Spanish War and there was a pause until Britain and France recommenced hostilities in 1803.
Spain declared war on Britain in December 1804; Portugal remained neutral until Spain and France signed the 1808 Treaty of Fontainebleau dividing Portugal between them. The Treaty of Badajoz contained a clause stating any breach of its terms rendered it void; Portugal declared the Treaty of Fontainebleau constituted such a breach and nullified the agreement. This is one reason Portugal disputes Spanish sovereignty over Olivença, the other being that its occupation contravenes the 1815 Treaty of Vienna.
In 2003, José Ribeiro e Castro, a Portuguese MEP raised the matter with the Council of Europe but while it remains an issue, it has not disrupted relations between the countries. In 2008, Olivenza and a number of other Portuguese and Spanish towns became part of the Euroregion of Extrem-Alentejo.