The Talmud Yerushalmi states that if a deer entered a house it is, of course, prohibited to close the door to trap the deer. However, if one closes the door for his own protection, as well as to trap the deer, it is permitted. The Rashba (R. Shlomo ben Aderet, 1235-1310, Spain) understands this at face value. That is, the fact that his intention in closing the door was also for his own sake, neutralizes the prohibition of trapping the deer. The Ran (R. Nissim ben Reuven of Gerona, 1320-1376, Spain) takes issue with this position and asks numerous questions on the Rashba. He asks how it can be permitted to close the door when the result of that action is inevitably that the deer will be trapped, especially since that is also his overt intention. He therefore offers an alternate explanation of the Yerushalmi.
The Acharonim, including the Ben Ish Hai (Rav Yosef Haim of Baghdad, 1833-1909), in his Rav Pa’alim and the Shilteh Giborim, attempt to explain the Rashba’s position. However, the Poskim, including Hacham Ovadia and the Menuhat Ahaba, do not rule in accordance with the Rashba. They hold that an additional intention for himself does not avoid the problem of trapping. This seems to be the opinion of Maran, as well, when he prohibits covering a beehive in order to protect the bees from the sun or rain, without any intent to trap the bees. Certainly then, in the case of the deer, in which he does want to trap, it is prohibited to close the door.
It is prohibited to close the door of a house with the intent to protect one’s self and also to trap an animal inside.