ST. LOUIS (AP) The 49ers had Austin Davis on the run. San Francisco counterpart Colin Kaepernick had all day.
San Francisco's defense took charge after spotting the St. Louis Rams 14 points, sacking Davis five times in the 49ers' 31-17 victory on Monday night.
Kaepernick wasn't sacked, and he threw for 343 yards and three touchdowns, putting the 49ers' comeback into high gear on an 80-yard strike to Brandon Lloyd.
On 3rd-and-six with 27 seconds left in the half, the 49ers (4-2) thought the time was right to take a shot. Lloyd put a double move on Janoris Jenkins and had clear sailing down the sideline for a touchdown with 14 seconds left that cut the Rams' lead to 14-10.
''It landed right in my lap,'' Lloyd said. ''It did a lot for our morale in the locker room.''
Ahmad Brooks and Dan Skuta had two sacks apiece for the 49ers, who have beaten the Rams (1-4) three straight times by double figures. They made the adjustment after being beaten early by unfamiliar plays and a quarterback some of them didn't know by name.
The 49ers have won three straight, hitting their stride after a 1-2 start. The Rams believed they were good enough to overcome another season-ending injury to quarterback Sam Bradford but have been a big disappointment in coach Jeff Fisher's third season.
Here are a few takeaways from the 49ers' victory:
The 49ers permitted one first down on St. Louis' first six possessions of the second half, taking a 24-14 lead. The Rams' lone decent possession led to Greg Zuerlein's 38-yard field goal that narrowed the gap to a touchdown with 2:24 to go, but San Francisco foiled an onside kick and then sealed the victory on Dontae Johnson's 20-yard interception return for a touchdown with less than a minute to go.
''Talk about an opportunity, I had the chance to make a play,'' Johnson said.
The pass rush was supposed to be a Rams strength, but they are still stuck on one sack for the season. Robert Quinn led the NFC with 19 sacks last year, and again was frustrated.
Kaepernick threw for 343 yards, the second-highest total of his career, topped only by a 412-yard effort in the opener last year against Green Bay.
Anquan Boldin and Michael Crabtree also had touchdown catches for the 49ers. Boldin led the way with seven receptions for 94 yards, including an 11-yard score in the third quarter.
COST OF VICTORY
The 49ers lost two starters and a backup to injuries in the third quarter Monday night.
Pro Bowl linebacker Patrick Willis (toe), guard Mike Iupati (concussion) and backup safety Jimmie Ward (quad) all were sidelined. San Francisco coach Jim Harbaugh didn't provide any updates.
Davis showed enough promise while Shaun Hill was out with a quadriceps injury to keep the starting job, and he threw for 300 yards each of the previous two games with six touchdown passes. The offense bogged down after gaining 151 yards in the first quarter.
He was 21 for 42 for 236 yards. Three of his four interceptions have been returned for touchdowns, all in the fourth quarter at home.
''We've had the ball in every game with a chance to either win or tie,'' Davis said. ''That's where hopefully my experience will help, the more opportunities I get.
''You've got to be able to go win games at the end.''
The Rams wore throwback uniforms - royal blue and bright yellow - to honor its 1999 Super Bowl title team, and they emulated that team but just for a short time. Guard Adam Timmerman joined wide receivers Isaac Bruce and Torry Holt for an impromptu bob n' weave TD dance in the end zone at halftime, and there wasn't much to cheer about after that.
The Rams blew a 21-0 lead against Dallas in their last home game and haven't had a winning season since the Greatest Show on Turf petered out in 2003.
The 49ers' comeback began on Phil Dawson's 54-yard field goal, his 10th in a row with four beyond midfield.
AP NFL website: www.pro32.ap.org and www.twitter.com/AP-NFL