Isn't Freemasonry a religion or a rival to religion?

Emphatically not.

Freemasonry requires a belief in Divinity and its principles are common to many of the world's great religions. Freemasonry does not try to replace religion or substitute for it. Every candidate is exhorted to practice his religion and to regard its Holy Book as the unerring standard of truth. Freemasonry does not instruct its members in what their religious beliefs should be, nor does it offer sacraments.

Freemasonry deals in relations between men; religion deals in a man's relationship with his God.

The prime qualification for admission into Freemasonry has always been a belief in Divinity. How that belief is expressed is entirely up to the individual.