HOW THE MEZUZAH IS POSITIONED IN THE DOORPOST [Mezuzah: position]
Gemara
(R. Chelbo): I saw Rav Huna roll his Mezuzah from "Echad" (which is towards the end of the first line, i.e. from the left) towards Shma (on the right).
33a (Rav Yehudah): If a Mezuzah was attached like a bolt, it is Pasul.
Question: R. Yitzchak bar Yosef related that all the Mezuzos in Rebbi's house were like bolts!
Answer: We distinguish between resting like a peg, or like Istavira (the shin bone).
33b Beraisa): If a doorframe was made of reeds, he cuts a tube-shaped opening and inserts the Mezuzah in it.
Bava Metzi'a 101b - Question: Who must supply the (place in which to put) the Mezuzah?
Answer (Rav Sheshes - Mishnah): The tenant supplies anything that does not require a craftsman.
One can put the Mezuzah in a hollow reed and hang it from the doorpost. This does not require a craftsman!
Rishonim
Rambam (Hilchos Mezuzah 5:8): If one carved into the doorpost and inserted the Mezuzah like a bolt that goes through rings on boards (of the Mishkan).
Kesef Mishneh: This is like Rashi (see the Rosh below).
Rosh (Hilchos Ketanos 19): Rashi explains that if it rests horizontally like a peg, it is Pasul. If it is vertical like the shin bone, it is Kosher. R. Tam disagreed, for it is not honorable to be vertical. This is called 'burial like a donkey' (Bava Basra 101b). In the Aron, the Sefer Torah and Luchos lied down. They were not standing, even though there was room for this. Their height equalled their width! When the Sefer Torah stands, everyone stands. When the Shali'ach Tzibur lies it down, everyone sits. (This shows that lying is its normal position - PF.) Also, if a doorframe was made of reeds, he cuts a tube-shaped opening and inserts the Mezuzah in it. If it were placed vertically, he could put it in one of the reeds of the doorframe (he would not need to cut a tube for it)! R. Tam explains that if it is vertical like pegs of the Mishkan, it is Pasul. If it is horizontal like Istavira, i.e. the ankle, which is in the width of the shin, it is Kosher. However, Yevamos 103a proves that Istavira is the (shin) bone which goes from the thigh to the foot. The Yerushalmi says that "Shma"must face Pnei ha'Bayis. Rashi explains that when it is rolled from Echad towards Shma, it is stood up so that Shma faces the hollow of the doorway, and not in (towards the house). R. Tam explains that the last line is positioned towards Reshus ha'Rabim, and the first line faces in, so Shma faces the hollow of the doorway, facing in. Similarly, R. Tam says that the Parshiyos of Tefilin should lie in the boxes, and not stand up. The custom is like Rashi. All Mezuzos of Rebbi's house were like a bolt. I.e. they were not sitting and not standing.
Note: Presumably the Rosh infers that they were unlike others', for if not R. Yitzchak would not have testified about them! Everyone else's were standing or sitting (according to Rashi and R. Tam, respectively,) so we must say that the Beraisa disqualifies a Mezuzah that is standing or sitting. Rebbi's Mezuzos must have been in a third position.
Rosh (ibid.): We must say that Rebbi's Mezuzos were not Pasul, and others' Mezuzos were not Pasul. Those of Rebbi's house were like Istavira, i.e. on a diagonal. They were not erect or lying. Alternatively, they were like a 'Nun'; part was erect, and part lying, like the shin connected to the foot. Others' Mezuzos were vertical according to Rashi; and horizontal according to R. Tam. The Mezuzos of Rabbeinu Meir me'Rotenburg were like a bent Nun.
Mordechai (Hilchos Tefilin 961): R. Tam says that erect is disgraceful. The Reish Galusa's household was punished for erecting the coffin of Rav Huna (Mo'ed Katan 25a). Also Parshiyos of Tefilin must lie down. We say that if the four Parshiyos were written on one parchment and put in the four boxes, he was Yotzei. Granted, if they lie down, the four boxes can be recognizable. If the parchments are erect, it is very hard to fulfill this. R. Elchanan says that Rashi can explain that the four Parshiyos were written along the length of the parchment, with space in between. (It is folded like an accordion; each Parshah stands up.) R. Tam ruled in practice to drill a hole in the width of the doorpost and insert the Mezuzah along the width, and to lie Tefilin Parshiyos along the width of the boxes. He said 'if I merit, I will build a wide Aron to enable lying down the Torah, the way it was in the Aron (in the Mishkan) and the way we read it.' R. Elchanan brought a proof for Rashi from the Yerushalmi, which says that Shma must face Pnei ha'Pesach. R. Tam must explain that Shma, and not the end of the Mezuzah, must face the doorway. Why did it mention Shma? The entire first verse must face the opening! Perhaps it merely meant the entire (top) side. That is difficult. Also, we do not read it like it was in the Aron! (Perhaps he means that in the Aron it was rolled closed.) Also Bahag says that they used to write 'Shakai' opposite the blank part of the Parshah, and there was a window in the (hollow) reed (in which the Mezuzah was placed), and there was a corresponding window (in the doorpost), so they could see 'Shakai' from the outside. This is unlike R. Tam. However, perhaps this is not obligatory; it is mere a Mitzvah to do so. Bahag says that Istavira is half erect and half lying down. The Ri explains that it is slanted, for a Mezuzah cannot be half erect and half lying down. The custom is like Rashi. Kidushin 22b supports this. It says 'just like the Mezuzah stands...'
Note: The word 'Mezuzah' really means doorpost. What is the proof that the parchment we affix to the Mezuzah must stand?! Perhaps he refers to the continuation 'also the door stands', and similarly, since the Torah says "write them on the Mezuzos", also the parchment must stand. Also, "Al Mezuzos Beisecha" connotes that the entire Mezuzah is on the doorpost. According to R. Tam, the end of the Mezuzah is not on the doorpost; it is merely near it. The bottom is far away! If one will carves a cavity in the doorpost to hold the Mezuzah (lying down), one who stands in the doorway cannot read it. (If the Mezuzah is along the width of the doorway, he can read it, but only if he turns his head to be horizontal.)
Terumas ha'Deshen (52): One puts the Mezuzah slanted. The top faces inside to the house. According to R. Tam, it must lie down with the length along the width of the doorpost, and the bottom faces Reshus ha'Rabim. Since we want to fulfill R. Tam's opinion, the top must face inside. It seems that this is because for one who reads it (from outside), the letters will be in order. If the top faces outside, this is not the way to read it for one who enters. We expound (that the Mezuzah is on the right side (looking from the outside) because we read) "Beisecha" like 'Bi'asecha' (the way you enter). This is even like Rashi, who says that it is vertical; Shma faces the doorway. One who enters needs to tilt his head only a little. If the top faced outside, he would need to turn his head back while reading. I wrote this because I saw many great Chachamim whose Mezuzos point outside.
Maharil (94): One should put the Mezuzah slanted; the top faces into the house. This is not (like the questioner thought,) in order that for one who reads it (from outside), the letters will be in order. Perhaps one reads it from the house! Rather, the Yerushalmi says that "Shma"must face the house. Since we slant it to fulfill also R. Tam's opinion, we must do like he held (Shma faces inside, like the Rosh brought).
Tosfos (Bava Metzi'a 102a DH b'Guvsa): Our Gemara supports Rashi, who says that we put the Mezuzah erect. Therefore, we need a reed to hold it, so it will not fall. R. Tam holds that we lie it down. If so, why is a reed needed? (We merely need to make a cavity in the doorpost to contain it!) We can say that the reed is to protect it from the moisture in the wall.
Poskim
Shulchan Aruch (YD 289:6): The Mezuzah must be straight, with the length along the length of the doorpost. He should position it so that Shma, i.e. the end of where it is rolled, is outside.
Beis Yosef (DH Asa'ah): A second Perush in Rashi explains that Istavira is (bent), just like the leg bone is erect above and the foot lies below. It is Kosher because one end is erect. R. Yerucham says that some hold that the Mezuzah should be slanted, and the bottom is slightly away from the wall.
Shach (9): In the Mechaber's region they did so. The Rema bring the custom in our regions.
Rema: This is the custom. However, some say that it is Pasul if it is erect. Rather, it must lie down, with the length along the width of the doorpost. Meticulous people fulfill both opinions, and place it on the diagonal. It is proper to do so. This is the custom in our lands. He should position it so that the top, i.e. Shma, faces inside, and the last line faces outside.
Gra (14): Meticulous people fulfill both opinions, like it says that they did in Rebbi's house. Letter of the law we follow Rashi, also regarding Tefilin, and so we do with the Sefer Torah (it stands in the Aron).
Beis Yosef (DH u'Mah): The Tur says that meticulous people put holds that Mezuzah partially erect and partially lying down, like a Nun. This is from Semak. The top should be closer to the inside, and the bottom leg (sticks out) towards the street, just like those who place it on the diagonal put the top inside, to fulfill also R. Tam's opinion. We hold like the Rambam and the first opinion of Rashi, that it stands.
Pischei Teshuvah (9): Yad ha'Ketanah says that when the doorpost has almost no width, one affixes it erect. The Rema: disagrees only when it is possible to fulfill both opinions. If one slants it, and most of the Mezuzah is not in the width of the dwy, most Poskim disqualify.
Pischei Teshuvah (10): Chomos Yerushalayim (146) says that if one cannot put the Mezuzah in the width of the doorway, he (puts it in on a slant the thickness of the doorpost); the bottom is near the opening (of the doorway), and the top is slightly away. Also R. Tam holds that the bottom should be near the opening. This is unlike Yad ha'Ketanah, unless we will say, with difficulty, that there is not even room to put it erect in the doorway.
Shulchan Aruch (OC 32:45): Each Parshah (of the head Tefilin) is out erect in its box.
Sha'arei Teshuvah (67): The Noda bi'Yehudah (2 OC 4) says that one need not wear Tefilin in which the Parshiyos lie down, like R. Tam says. This is unlike the argument about the order of the Parshiyos, in which Rav Hai Gaon and other Ge'onim hold like R. Tam. Also, perhaps even R. Tam does not disqualify b'Di'eved if the Parshiyos are erect.