ST. LOUIS -- The 49ers were good, bad and really ugly Sunday, but for the second week in a row, they did just enough to earn a gritty division win.
The key play for the 49ers came on fourth and one in the third quarter when running back Frank Gore, still grieving over the loss of his mother four days earlier, took a handoff, ran left and then broke four tackles on a 43-yard scamper to the end zone.
"They had nine in the box -- everybody was up there," said Gore, who rushed for 81 yards and two scores. "I got through and kept pumping my legs."
The touchdown put the 49ers ahead by a point, and an opportunistic defense that hemorrhaged yards but didn't give up many points helped the team hold on for a 17-16 win. The 49ers are 2-0 for the first time since 1998.
Never miss a local story.
"We're frustrated because it's ugly right now," said guard Justin Smiley, who had a key block on Gore's run. "It's sputtering. Sometimes it looks good, sometimes it looks bad."
The offense looked decidedly horrid at game's end.
Down 16-14 late in the fourth quarter, the 49ers got a break when the Rams' Dante Hall misplayed a punt and Marcus Hudson pounced on it at the St. Louis 25-yard line.
But as was the case in last week's win over Arizona, the offense showed little imagination when it had an opportunity to take over the game. The 49ers ran three consecutive times and gained four yards before Joe Nedney trotted onto the field to kick what turned out to be a winning 40-yard field goal. Afterward, head coach Mike Nolan said he instructed offensive coordinator Jim Hostler to play for the three points.
"If I had to do it again, I probably would have done something on the third-down play because we were already in field-goal position at the time -- maybe take a shot," Nolan said. "But I'm glad we did what we did because we won the game."
The 49ers were equally unadventurous on their next possession, which began on the St.Louis 47 when the Rams failed to convert on fourth down.
Again, San Francisco ran three consecutive times, calls that were foreshadowed by the presence of David Baas, a backup offensive lineman who was filling in at tight end for injured Billy Bajema. Baas said afterward he hadn't played tight end since high school.
The 49ers punted and the Rams took over on their 20 with two timeouts and 1:53 remaining.
Quarterback Marc Bulger immediately hit receiver Isaac Bruce on back-to-back nine-yard outs and later connected with Drew Bennett on a 12-yard pass to the San Francisco 45. Bulger had 1:15 to work with at that point but decided to spike the ball on first down to stop the clock.
That decision proved costly for St. Louis.
On second down, Bryant Young sacked Bulger for a seven-yard loss.
On the next play, Bulger went back to Bruce, who finished with a game-high 145 yards receiving, for a 14-yard gain that was three yards shy of a first down.
On fourth down, the Rams sent in kicker Jeff Wilkins, who connected from 53 yards earlier in the fourth quarter. This attempt, however, was two yards longer and it proved to be too much for Wilkins. As the ball sailed just beneath the crossbar, Nolan pumped his fist on the 49ers' sideline.
"That builds character," Nolan said of his second consecutive nail-biting win. "That's better than kicking the hell out of the other guys."
Bulger was 24 for 41 for 368 yards and one touchdown. He completed passes to seven receivers. Rams running back Steven Jackson gained just 60 yards on 21 carries.
St. Louis lost despite outgaining the 49ers 392-186. The Rams had 20 first downs to eight for the 49ers.
"We've got to find ways when we've got a lead, lead at halftime, lead in the third quarter, to sustain it," Rams coach Scott Linehan said. "You can't have opportunities and squander them. And we did."
The Associated Press contributed to this report.