Mostly they weren’t. Even today, railway lines aren’t really defended – not because no one thinks to target them, but more because it’s trivially easy to repair long stretches of open rail.
For example, in the ongoing Ukraine war, Russia is very reliant on railways to supply their troops. Ukraine mostly doesn’t bother trying to attack the tracks themselves, because it’s basically a waste of precious ammunition. Even if they can hit the tracks (which they can with precise Western munitions, if the tracks are within a certain distance), Russia can simply repair the tracks in a matter of hours.
Stations and bridges are still worth attacking, but since they are fixed points they also tend to be much better defended.
Latest Answers