There's no recipe for beating the 49ers as much as there is a recipe for beating any team.
Physical defense and balanced offense.
Worked for the Giants, Vikings, Rams, Seahawks and I don't need to remind you of the Ravens.
Niners thrash teams that are one dimensional, which is a large portion of the league. Pats, Packers, Saints, Lions, Bears. Pound on the Jets, Bills, Cards and such, but even the Dolphins at the Stick, kind of gave the Niners a game with that method of attack.
The opponent doesn't have to be exceptional with their offense, just "good" enough in both aspects to force the Niners into playing straight up defense. If you are a pass-happy team, the Niners will kill your QB, if you are a power run team, the Niners will stuff the middle and contain on the edge, but if you can have something resembling success in both? Now you are working that defense and keeping that offense on the sidelines. When they come out out of rhythm against your physical defense, you can be disruptive and create problems for the Niners.
Its no secret, it works against most teams, but if the Niners are going to lose, that is how.