A person is permitted to pray facing the bathroom, as long as the door is closed and no foul odor reaches him. However, if the door is open, he is prohibited from praying there (Mishnah Berurah 83:5).
Feces of young infants do not emit a stench and therefore the law regarding them is unlike that of excrement. When a child reaches the age that he can eat a kezayit (an olive’s volume of food) produced from grain in the amount of time it takes to eat half a loaf of bread (approximately 6-7 minutes), it is necessary to distance oneself from his excrement, just as one is obligated to do regarding an adult’s feces (Shulchan Aruch 81:1). There are those who write that this starts at one year of age. All this pertains to extenuating circumstances; l’chatchilah it is good to distance oneself from the excrement of a baby of any age, even one who is eight days old (Mishnah Berurah 81:3; Kaf HaChaim 1:6).
When praying near a baby at least one year old who defecates in his diaper, it is proper to initially verify that no foul odor is present. As long as no foul odor exists, it is permissible to pray next to him, for even if he did defecate, since he is covered in a diaper and clothing, and no scent reaches the person reciting matters of sanctity, there is no prohibition to pray there (see Halichot Shlomo, Tefillah 20:4-5). If a bad smell did reach the one praying and despite this he continued to pray, he must go back and repeat his prayer.
At times, a sewer near the synagogue emits a bad smell which may reach the synagogue, in which case it is prohibited to pray inside. Occasionally, closing the windows that face the sewer helps, but when the foul odor lingers, spraying air freshener can be used to eliminate it. In the past, people would offset the smell by burning a garment (Kaf HaChaim 79:20).
The laws concerning distancing oneself from different odors depend on the accepted local norms. In the past, sewage, including feces and urine, would flow down the sides of the streets and the air in the densely populated cities was full of foul odors. Even so, prayers would be recited in the synagogues and in the houses near the sewage canals. Because everyone was accustomed to them, those routine smells were not considered foul. Only on summer days, or when the sewage canals were blocked, would the foul odor intensify and people would not pray where it existed (see Mishnah Berurah 79:5). However, nowadays, sewage is drained, the air is purer, and we are more sensitive to bad smells, hence, wherever we sense a foul odor by today’s standards, it is forbidden to pray.
In rural areas with barns, the smell that wafts from the barns to the houses and the synagogue is not offensive to those who live there. However, in the city, this odor may be considered foul, and it would be prohibited to pray as long as it persists. Those who are guests in communities like these should follow the custom of the place.
People who pray outside must be careful not to pray in close proximity to garbage bins that smell. Even when the garbage cans do not emit a foul odor, it is proper not to pray within four amot of them or when they are directly in front of the person.