HUNTSVILLE, Ala.—President Donald Trump traveled to Alabama on Friday to shore up a flagging Senate candidate, injecting himself into a contentious special election that puts him at odds with a broad swath of conservatives and tests the loyalty of his long-unshakeable base.

The president addressed a rally in support of Sen. Luther Strange—a fellow Republican with strong ties to the party establishment—over a conservative insurgent, former state Supreme Court Chief Justice Roy Moore, who is backed by the president’s former...