The gastric pathogen, Helicobacter pylori bacteria have to swim across a pH gradient from 2 to 7 in the mucus layer to colonize the gastric epithelium. Previous studies from our group have shown that porcine gastric mucin (PGM) gels at an acidic pH < 4, and H. pylori bacteria are unable to swim in the gel, although their flagella rotate. Changing pH impacts both the rheological properties of gastric mucin and also influences the proton (H+)-pumped flagellar motors of H. pylori as well as their anti-pH sensing receptors. To unravel these intertwined effects of acidic pH on both the viscoelastic properties of the mucin-based mucus as well as the flagellar motors and chemo-receptors of the bacterium, we compared the motility of H. pylori in PGM with that in Brucella broth (BB10) at different pH values using phase contrast microscopy to track the motion of the bacteria. The results show that the distribution of swimming speeds and other characteristics of the bacteria trajectories exhibit pH-dependent differences in both media. The swimming speed exhibits a peak at pH 4 in BB10, and a less pronounced peak at a higher pH of 5 in PGM. At all pH values, the bacteria swam faster and had a longer net displacement in BB10 compared to PGM. While the bacteria were stuck in PGM gels at pH < 4, they swam at these acidic pH values in BB10, although with reduced speed. Decreasing pH leads to a decreased fraction of motile bacteria, with a decreased contribution of the faster swimmers to the distributions of speeds and net displacement of trajectories. The body rotation rate is weakly dependent on pH in BB10, whereas in PGM bacteria that are immobilized in the low pH gel are capable of mechano-sensing and rotate faster. Bacteria can be stuck in the gel in various ways, including the flagella getting entangled in the fibers of the gel or the cell body being stuck to the gel. Our results show that in BB10, swimming is optimized at pH4, reflecting the combined effects of pH sensing by anti-pH tactic receptors and impact on H+ pumping of flagellar motors, while the increase in viscosity of PGM with decreasing pH and gelation below pH 4 lead to further reduction in swimming speed, with optimal swimming at pH 5 and immobilization of bacteria below pH 4.