Mr. Trump’s decision to back Mr. Strange in the first place ran counter to the advice of some of the president’s advisers, who had privately argued that Mr. Trump should not expend precious political capital in support of a candidate who could lose. More recently, Stephen K. Bannon, his former chief strategist, has publicly urged Mr. Trump to abandon Mr. Strange, portraying him as a lieutenant of Mr. McConnell and the personification of the “swamp” of establishment Washington that the president has condemned.

Only hours before Mr. Trump was to appear in Huntsville, his own secretary of Housing and Urban Development, Ben Carson, issued a statement praising Mr. Moore as “someone who reflects the Judeo-Christian values that were so important to the establishment of our country.” It underscored the degree to which Mr. Trump’s endorsement of Mr. Strange has driven a wedge between the president and his core supporters, who flocked to his anti-establishment message and many of whom are backing Mr. Moore.

Yet Mr. Trump, appearing at ease in campaign mode after a week of scripted speeches and diplomatic maneuvering at the United Nations General Assembly, thrilled Alabamians with tough talk about national security and immigration, and dismissing as a “hoax” the notion that Russia meddled in the 2016 election despite the unanimous consensus of the nation’s intelligence officials.

He indulged himself on several pet topics that were of no apparent help to Mr. Strange, including the president’s Electoral College success, his decision to work with congressional Democrats and what he argued was the growing penchant of N.F.L. referees to throw flags for hard hits.

The digressions were part of a meandering, more than hourlong speech, interspersed with scripted praise for Mr. Strange and extemporaneous declarations of wonder about the sheer size of the 6-foot-9 senator known as Big Luther.

“That is the tallest human being I’ve ever seen,” Mr. Trump said at one point.

“Luther wants to end business as usual, stop the insider dealing, and Luther is determined to drain that swamp,” Mr. Trump said. “Luther has proved that he’s not beholden to anyone.”