Your confounding how things should be with how they are. These are two distinct philosophies, only one of which is relevant in most of the real world, unfortunately.
Punishment and rehabilitation aren’t mutually exclusive. Arguably, the punishment aspect is served by the removal of freedoms that being locked in prison entails. Rehabilitation can be any number of things that the prisoner does while in prison.