Rafi was originally in the service of the Tahirid dynasty, which controlled Khurasan as virtual viceroys in the name of the Abbasid Caliphate. Tahirid control of Khurasan was challenged in the 860s by the revolt of Ya'qub al-Saffar, who, beginning from his home province of Sistan, defeated the Tahirid armies and by 873 had seized the provincial capital, Nishapur. The failure of Ya'qub to seize Baghdad in 876, however, and his death soon after weakened the Saffaridregime that his brotherAmr ibn al-Layth inherited. With the Saffarids engaged elsewhere, and their takeover of the former Tahirid domains condemned as illegal by the Abbasid government, Nishapur was seized in 875 by an anti-Saffarid faction under Ahmad ibn Abdallah al-Khujistani, a former Tahirid soldier. After his murder in 882, Rafi, who had risen to be his commander-in-chief, was acclaimed by al-Khujistani's army as his successor. Rafi faced the opposition of the Saffarids under Amr, who had made his peace with the caliph in 879 and been recognized as governor of Khurasan and most of Iran, as well as the adventurer Abu Talha Mansur ibn Sharkab, who had briefly seized Nishapur in 876–878 and was now ruler of Merv. Defeated, Abu Talha soon sought Samanid and Saffarid aid, and in 885 he was appointed as the Saffarid deputy for Khurasan, while Amr turned again west to confront the attempts of the Abbasid regent, al-Muwaffaq, to recover Fars. At this point, Rafi's fortunes changed, as al-Muwaffaq stripped the Saffarids of their governorships, and conferred Khurasan on Rafi. Rafi was now able to secure an alliance with the Samanids of Transoxiana and neutralizeAbu Talha, seizing Merv and Herat. He raided as far as Khwarezm in 886 and aided the Samanid Isma'il ibn Ahmad against his brother Nasr I. In 888/889 Rafi invaded and conquered the Zaydid domains of Gurgan and Tabaristan, defeating the Zaydid ruler Muhammad ibn Zayd in battle at the Chalus River. From Tabaristan he marched to Qazvin and then Rayy in 889/890, where he established his headquarters until al-Muwaffaq's death in June 891. During his stay in Tabaristan, Rafi was joined by Ali ibn al-Layth, Amr's brother, who had initially been the favoured candidate to succeed Ya'qub. Ali's sons, al-Mu'addal and al-Layth, would later accompany Rafi in his attempt to regain control of Khurasan in 896. With the death of al-Muwaffaq and the rise to the regency of his son, al-Mu'tadid, Rafi's fortunes changed again: al-Mu'tadid's policy of re-establishing caliphal authority in the western Caliphate required him to maintain friendly relations with Amr ibn al-Layth in Iran, and Rafi's control of Rayy threatened the province of Jibal, which al-Mu'tadid soon recovered from its semi-independent Dulafid governors. As a result, al-Mu'tadid invested Amr with the governorship of Khurasan, ordered Rafi to evacuate Rayy, and sent an army under Ahmad ibn Abd al-Aziz ibn Abi Dulaf against him. Evicted from Rayy, in order to gain allies for an attempt to recover Khurasan, Rafi reconciled himself with Muhammad ibn Zayd, to the extent that he had the Friday prayerread in the latter's name. This turn towards the Alids marked a public break with the Abbasid and Sunni camp, which the Saffarids exploited accordingly to bolster their support among the populace of Khurasan. In 896, Rafi invaded Khurasan and captured Nishapur, where too he had the prayer read in Muhammad's name. He even adopted Alid white instead of Abbasid black for his colours. The help promised by Muhammad ibn Zayd, however, never arrived, and Rafi was soon expelled from the city by the Saffarids. His forces were defeated at Bayhaq and Tus, and finally driven out of Khurasan to Khwarazm, where Rafi was defeated and killed in a final battle. His severed head was dispatched to Baghdad.