The short-handed 49ers hung on for a thrilling 26-23 victory over Matthew Stafford and the Rams on Thursday night.