On the American frontier of 1874, a new railroad will soon be rerouted through Rock Ridge, in order to avoid running through quicksand. Realizing this will make Rock Ridge worth millions, the conniving attorney general Hedley Lamarr (named after Hedy Lamarr) wants to force Rock Ridge’s residents to abandon their town, and sends a gang of thugs, led by his flunky Taggart, to shoot the sheriff and trash the town. The townspeople demand that Governor William J. Le Petomane (named after Le Petomane) appoint a new sheriff to protect them. Lamarr persuades the dim-witted Le Petomane to appoint Bart, a black railroad worker, who was about to be executed for assaulting Taggart earlier. A black sheriff, he reasons, will offend the townspeople, create chaos, and leave the town at his mercy.
After an initial hostile reception (where Sheriff Bart has to take himself as a hostage to escape), he relies on his quick wits and the assistance of Jim, an alcoholic gunslinger known as the “Waco Kid,” to overcome the townspeople’s hostility. He subdues Mongo, an immensely strong, dim-witted, but philosophical henchman sent to kill him, then he beats German seductress-for-hire Lili von Shtüpp at her own game, with Lili falling in love with him. Upon Mongo’s release, he vaguely informs Bart of Lamarr’s connection to the railroad, so Bart and Jim visit the railroad work site and discover from Charlie, Bart’s best friend, that the railway is planned to go through Rock Ridge. Just as Taggart and his men arrive to kill Bart, Jim outshoots the thugs, forcing Taggart to retreat to Lamarr. Lamarr, furious that his schemes have backfired, hatches a larger plan involving a recruited army of thugs, including common criminals, Ku Klux Klansmen, Nazis, and Methodists.
East of Rock Ridge, Bart introduces the white townspeople to the black, Chinese, and Irish railroad workers, who have agreed to help in exchange for acceptance by the community, and explains his plan to defeat Lamarr’s army. They labor all night to build a perfect replica of their town, as a diversion; with no people in it, though, Bart realizes it will not fool the villains. While the townspeople construct replicas of themselves, Bart, Jim, and Mongo buy time by constructing the “Gov. William J. Le Petomane Thruway”, forcing the raiding party to turn back for “a shitload of dimes” to pay the toll. Once through the tollbooth, the raiders attack the fake town populated with dummies, which are booby-trapped with dynamite bombs. After Jim detonates the bombs with his sharpshooting, launching bad guys and horses skyward, the Rock Ridgers storm the villains.
The resulting brawl between townsfolk, railroad workers, and Lamarr’s thugs literally breaks the fourth wall as the fight spills onto a neighboring set, where director Buddy Bizarre is directing a Busby Berkeley-style top-hat-and-tails musical number; then into the studio commissary for a food fight; and then out of the Warner Bros. film lot into the streets of Burbank. Lamarr, realizing he has been beaten, hails a taxi and orders the driver to “drive me off this picture”. He ducks into Grauman’s Chinese Theatre, which is playing the premiere of Blazing Saddles. As he settles into his seat, he sees Bart arriving on horseback outside the theatre. Bart blocks Lamarr’s escape, and then shoots him in the groin. Bart and Jim then go into Grauman’s to watch the end of the film, in which Bart announces to the townspeople that he is moving on because his work in Rock Ridge is done (and he’s bored). Riding out of town, he finds Jim, still eating his popcorn, and invites him along to “nowhere special”. The two friends ride off into the sunset—in a chauffeured limousine.