Prison primarily serves as punishment which among many other things serves to deter other criminals as well as to remove dangerous individuals from the general population. Rehabilitation is a secondary goal of prison, not its primary purpose.
The modern prison system, used for the last few centuries, was indeed invented primarily for rehabilitation. Prisons were previously used for holding prisoners until trial and/or sentencing could be carried out.