SANTA CLARA – Robert Saleh knows he was not Kyle Shanahan’s first choice as 49ers defensive coordinator. But, now, Shanahan has come to conclude Saleh might have been the best choice for the job.

After Shanahan was named 49ers head coach in February, the clock was ticking for him to put together his staff. Former Jacksonville Jaguars coach Gus Bradley accepted the coordinator job with the San Diego Chargers. And the Chicago Bears would not allow Vic Fangio out of his contract to return to the 49ers.

But with two games remaining in his first season as 49ers’ defensive coordinator, Saleh has proven himself and will unquestionably be back in the same role next season, Shanahan said.

“Yeah, there’s no doubt about that,” Shanahan said on “49ers Game Plan,” which is scheduled to air Saturday at 3 p.m. on NBC Bay Area (Ch. 3).

“Robert has been great. I think he’s gotten better throughout the year, and I think he’ll be even better next year. Robert has a great future ahead of him. And I feel very fortunate that he’s the coordinator for the Niners.”

Saleh, appearing on the 49ers Insider Podcast during training camp, said he knew he was not the top person on Shanahan’s list to become defensive coordinator after serving three seasons as Jacksonville’s linebackers coach. But he said he was confident if he got an interview, he would convince Shanahan of his worthiness to lead the defense.

And that is exactly what he has accomplished while implementing his 4-3 scheme. The 49ers were ranked last in the league a year ago. This season, the 49ers' defense is holding the opposition to 56 fewer yards per game and is 22nd overall.

The 49ers’ defense is playing its best football in the second half of the season. A defense that was among the worst in league history stopping the run a year ago has now turned in four consecutive games of holding the opposition to fewer than 100 yards rushing.

“I think Robert has done a great job,” Shanahan said. “It’s his first year as a coordinator, and I know he’s waited for this time for a while. He’s been everything I hoped for and more. He’s very smart with how he’s run our scheme. I’ve been excited with our scheme.”

The 49ers’ improved defensive play has coincided with better continuity – the result of fewer injuries forcing starters to leave the lineup.

“He’s really worked through a lot of things this year with moving people around with the injuries we’ve had and trying to find the best spots for guys,” Shanahan said. “I think it’s not a coincidence this last month or so we’ve had the most consistent play with the same people out there. If you look through our first 10 weeks, it seemed like a new lineup almost every week.

“And the more guys can play together, the more you can run the same coverages, the same fronts, the same blitzes every week-in and week-out, and that’s when you can start improving.”