Caerphilly is Wales' greatest castle, and unlike most of the huge Welsh castles it was not built by Edward I. The builder was Gilbert de Clare, Earl of Gloucester, alias the Red Earl -- some say because of his red hair, others because of his temper, but really we have no idea. De Clare was one of England's mightiest barons, and he began building this fortress in 1270 to secure control of his lands in south Wales.
The story of the English conquest of Wales is, on the one hand, grimly simple -- the English were stronger, and the Welsh had something they wanted -- but on the other complex and highly personal in the best medieval style. As soon as they had England under their control in 1067, some of the Normans began crossing into Wales, as if carried onward by their momentum. They overran some low-lying areas, which became the March of Wales; the lords of the March owed allegiance to the King of England, but the March was not part of England and English law did not run there. The Welsh held onto the mountains, some isolated valleys, and the northwest coast, and whenever the English were weak they swept back down into the lowlands.
When civil war broke out in England in the 1260s the strongest prince in Wales was Llewellyn ap Gruffyd of Gwynedd. Llewellyn rallied the Welsh behind him, defeated the Marcher lords in several battles, and reconquered many disputed areas. (On the map, green is Llewelyn's inheritance, blue the other Welsh lords, purple the land he reconquered from Marcher lords.) Since the government of Henry III was more worried about rebellious barons than the Welsh, Henry signed the Treaty of Montgomery with Llewelyn in 1267 recognizing the boundaries above. Llewelyn seemed to have secured Welsh independence for another century.
Meanwhile, Gilbert de Clare had been a major figure in that English civil war I mentioned. First he was on the side of the rebel barons, helping Simon de Montfort win the victory over the king that made him effective ruler of the realm. But then he switched sides and joined the party of Prince Edward, the future Edward I, helping him to win the Battle of Evesham that broke Montfort's power and put the royal family back in charge. As his reward for his clever trimming de Clare was granted yet more lands in Wales. On some of this land, in 1270, he began building Caerphilly.
Llewellyn was having none of that, and in 1272, while Edward I was out of the country on crusade, he swept down and destroyed the building works at Caerphilly. No castles would be built in Wales without his permission, he said. But Llewellyn overreached. His pretensions angered both the Marcher lords and many of the Welsh. In 1274, his brother Owain tried to assassinate him. In 1277, many Welsh lords joined the English in a war that saw Llewellyn lose all he had gained in the 1260s. Then in 1282, in the midst of further fighting, Llewellyn was ambushed and killed. (The Welsh say by treachery, the English say not so.) After Llewellyn's death Edward I proclaimed himself Prince of Wales -- he later passed this title to his eldest son, beginning that tradition -- and began the campaign of conquest that ended Welsh independence. Llewellyn is remembered as "Llewellyn the Last."
Gilbert de Clare finished building Caerphilly in the 1280s. Surrounding a core that provided luxurious living space -- by northern European standards of the 1200s, anyway -- were massive walls and, beyond that, two large ponds created by fortified dams. (Above is the great hall.)
The castle was besieged in 1294 during a Welsh revolt, and again in 1326 during the overthrow of Edward II -- it passed via marriage to Hugh Despenser, Edward's favorite -- but it never fell. In 1486 it came by inheritance to Jasper Tudor, and then when his son became Henry VII it fell to the crown. After that it was ignored, since the kings had other Welsh castles they liked better, and fell into ruin.
In 1776 the castle as purchased by the Marquess of Bute, John Stuart. The Marquesses of Bute owned a lot of land in South Wales that was nearly useless for farming but turned out to be full of coal. As South Wales became one of the main centers of the Industrial Revolution, the Marquesses grew hugely wealthy. They invested some of their profits in restoring a range of historic properties in Wales, including this castle. They bought out dozens of houses that had encroached on the grounds and demolished them, and they cleared the ponds with the aim of reflooding them. That did not actually happen, though, until after the castle was given back to the state in 1950.