Steve Evans will be back at Peterborough United next season.
Evans, who was sacked as Posh boss in January after a turbulent 11-month reign, will be appointed manager of League One rivals Gillingham on July 1.
Evans has long been the favourite to take over at Priestfield following the dismissal of Steve Lovell in April.
Evans left Posh sixth in League One when he was dismissed because of an apparent personality clash with chairman Darragh MacAnthony.
Gillingham chairman Paul Scally said: “I fully expect him (Evans) to bring us success both on and off the field.”
Gillingham finished 13th in League One last season. Evans, who lives in Wansford near Peterborough, has previously won promotion from the division with Rotherham United