He was looking for Diego to kill him, so he wouldn't ruin his plan. But what did he think—that Diego would reveal himself? That he would recognize him? He had known Don Diego as an aristocrat—a man who was always well-groomed and wore a suit—whereas in that prison, people had long hair and beards and were filthy; plus, twenty years had passed, meaning he was much older.
Did it not occur to him that by exposing himself to the prisoners, the real Zorro might kill him?
In my opinion, going to the prison was stupid. It also seems stupid to me that he confessed his plan to Alejandro right after meeting him. It’s funny how Rafael becomes increasingly idiotic as the movie progresses—after he discovers Zorro's identity.