Production and release
According to Rihanna, Timberlake wrote the song's lyrics "in his head. He didn't write anything on paper. He went into the booth and sang it."
"Rehab" is one of the three songs composed and produced by Timbaland for Rihanna's third studio album, Good Girl Gone Bad. Timbaland was on tour with Justin Timberlake to promote Timberlake's 2006 album FutureSex/LoveSounds, which Timbaland had also produced. After completing a show in Chicago, they joined Rihanna in the studio, where Timberlake experimented with beats and melodies. Weeks later, the three got together in New York City, where Timberlake had already conceptualized a song for Rihanna. Timbaland, who wanted to compose a song called "Rehab" for Rihanna, was producing a beat, when Timberlake came in and improvised the song over it. Hannon Lane co-wrote and co-produced the song, and Demacio Castellon recorded and mixed it in 2007 at Roc The Mic Studios in New York City. Rihanna's stint with Timbaland also resulted in "Sell Me Candy" and "Lemme Get That", produced for Good Girl Gone Bad.
Timberlake told Entertainment Weekly that he believed "Rehab" to be "the bridge for [Rihanna] to be accepted as an adult in the music industry". Rihanna told Robert Copsey of Digital Spy that she enjoyed working with Timberlake, and learned much from the sessions. She said, "Working with Justin in the studio is just great. He's a fun guy and likes to make all the sessions enjoyable. He's also such a genius when it comes to lyrics."
"Rehab" was the eighth and final single to be released from Rihanna's third studio album Good Girl Gone Bad (2007). According to American music magazine Rap-Up, both "Breakin' Dishes" and "Rehab" were potential single releases, but "Rehab" was chosen. Def Jam Recordings released the song for airplay on contemporary hit radio in the United States on October 6, 2008. On November 3, 2008, it was released to rhythmic contemporary and urban contemporary radio stations. The single was released in the UK by Mercury Records on December 8, 2008, as a CD single containing both the album version and an instrumental version of "Rehab". On the same date, it was released as a digital download in Ireland. A live performance of the song, recorded in Manchester during Rihanna's Good Girl Gone Bad Tour (2007–09), was released for download on the UK and Ireland iTunes Stores. The performance was featured on Rihanna's DVD Good Girl Gone Bad Live.
On December 12, 2008, the song and its instrumental version were released on iTunes and 7digital in ten countries, including Australia, Italy, New Zealand, Spain and Switzerland. In January 2009, it was released as a CD single in Germany, with the same material as in the UK release. In early 2009, Timbaland signed a contract with Verizon Communications, under which he would create music exclusively for BlackBerry Storm mobile phone owners. He was contracted to work with various artists and remix their songs, which would be available for Verizon customers to download at no extra cost. "Rehab" was the first song remixed for the project, and this version was released in Canada and the US on May 19, 2009, via iTunes.