Ah, excellent question. Herod knew that Jesus was in Bethlehem. This is where he executed his order (Matthew 2:16). John lived in the hill country (Luke 1:39), or Ein Karem by tradition. This is near Jerusalem, about seven miles from Bethlehem. It is possible he escaped the slaughter due to his distance from Bethlehem. If you go to Ein Karem, however, there is a rock that he was supposedly hidden behind. Not sure how true that is, but that is the tradition.