FOR 30 years, the tradition has been for each newly elected president of the Is lamic Republic to travel at once to Mashad, Iran’s main “holy” city, to thank Ali bin Mussa, the only one of Shiism’s 12 imams buried in Iranian soil.

When he was first elected president four years ago, Mahmoud Ahmadinejad made much of the Mashad trip by spreading rumors about his “secret” connections with the imams. This year, the state-owned media were presented his new pilgrimage as a “working visit” during which the imam would review and approve the president-elect’s program.

But Ahmadinejad was forced at the last minute to scrub the trip. Angry Mashadis, many of whom believe Ahmadinejad stole the election, were determined to give him a rough reception. Despite the presence of some 15,000 Basij security men in the city, the authorities couldn’t guarantee the president’s safety — let alone deliver the enthusiastic, welcoming crowds that he requires for propaganda purposes.

The canceled pilgrimage is the latest sign of trouble in the wake of Ahmadinejad’s controversial re-election.

Last Friday, his visit to Shiraz, Iran’s cultural capital, was called off on “security grounds.” On Monday, the Foreign Ministry canceled what was to be the first-ever visit by an Iranian leader to Libya.

Planned a year in advance, that trip was to feature Ahmadinejad signing a “treaty of friendship and cooperation” with the African Union and addressing a summit of African leaders. (Yesterday, it was announced that the visit would happen, somewhat shortened — but then it was called off again.)

On Tuesday, it was announced that yet another of Ahmadinejad’s “historic visits” was off — he will not travel to Sharm-el-Sheik in Egypt to address the summit of the nonaligned movement after all.

Since 2007, Ahmadinejad has campaigned to lead the “nonaligned” group and to host its next summit in Tehran in 2012. His “brother,” Venezuelan President Hugo Chavez, has visited a dozen capitals to win support for the Iranian bid — efforts now clearly all in vain. Wounded politically at home, perhaps mortally, Ahmadinejad is in no position to claim an international leadership position.

Another long-planned trip, visits with six leftist leaders in their Latin American capitals, already postponed once because of the election imbroglio, was definitively back-burnered yesterday.

With his re-election approved by the Council of the Guardians of the Constitution, Ahmadinejad may be out of immediate danger. But he’s a diminished figure even if he manages to hang on.

Four years ago, he won as a populist candidate who owed no debt of gratitude to “Supreme Guide” Ali Khamenei (who had tacitly endorsed another candidate). His prestige was further enhanced when, in a second round of voting, he crushed former President Hashemi Rafsanjani, a grandee of the Khomeinist nomenklatura. Ahmadinejad could claim that he was his own man, relying on no establishment cabals.

The June 12 election changed all that. Whatever the truth of allegations about massive fraud, the fact is that many Iranians, perhaps a majority, believe that Ahmadinejad didn’t win. To many, his re-election seems merely the result of plotting by a power clique centered on Khamenei.

“Ahmadinejad is president not because people voted for him, but because Khamenei says so,” notes Tehran analyst Saeed Hajjarian.

Worse still, the authorities have had to deploy tens of thousands of security agents, kill dozens of protestors and imprison more than 4,000 dissidents to prevent Ahmadinejad’s victory from being challenged by millions of protest marchers every day.

He governs thanks to the say-so of the “supreme guide” and the batons and bayonets of the security agents — not what populist Ahmadinejad had hoped.

His legitimacy is challenged at all levels of Iranian society, including every segment of the Khomeinist establishment. He has to invoke Khamenei’s authority in support of every move he makes. He is the first Islamic Republic president to have split the Khomeinist camp so deeply, and perhaps permanently.

With the presidency’s loss of legitimacy, the “republic” part of the “Islamic Republic” may have gone forever. The emerging system could be called an “imamate,” with the “supreme guide,” who now calls himself “the living imam,” running the show. In that case, the re-elected president no longer needs to go to Mashad to communicate with an imam who died 1,200 years ago.

Amir Taheri’s new book is “The Persian Night: Iran Under the Khomeinist Revolution.”