Oscar Wilde was transferred to Her Majesty’s Prison Reading, Berkshire, in November of 1895. He remained there until his release from prison on May 19, 1897. He wrote De Profundis while at the prison, taking it with him when he left.
He wrote The Ballad of Reading Gaol after his release. It was published under a pseudonym, and a rather obvious one at that: his identifier in Reading Gaol, C.3.3. That was his cell number: block C, floor 3, cell 3. The hanging he writes about in this ballad was “inspired” by one he witnessed at the prison. Charles Thomas Wooldridge was executed for the murder of his wife; Wilde dedicated the poem to “C.T.W.”