In our previous study, we demonstrated that Shen-ling-bai-zhu-san (SL), a classical Chinese herbal formula, could alleviate lactose-induced diarrhea. However, little is known about the mechanism underlying SL action or the efficacy of the polysaccharide (PL) derived from SL. In this study, we investigated the effect of SL and PL on improving the dysregulated luminal and mucosal microbiota in rats with high lactose diet using 16S rRNA analysis. The concentrations of lactose, lactic acid in cecum and short-chain fatty acids (SCFAs) in cecum and portal vein were measured, meanwhile the expression of ion transporters were ascertained. Our data suggest that the SL, PL and cecal microbiota transplantation (CMT) significantly decreased fecal water content and water intake. In the luminal microbiota there was a significant increase in Akkermansia, Bifidobacterium and Blautia and a lower abundance of Lactobacillus, Escherichia-Shigella, and Dubosiella, while the mucosal microbiota showed a significant increase in Bifidobacterium, Akkermansia, Albaculum, Bilophila, and Coriobacteriaceae_UCG-002 and a lower abundance of Enterococcus, Helicobacter, Dubosiella, and Collinsella. Furthermore, the treatments enhanced lactose fermentation and SCFA production, which may be related to the modulation of the luminal microbial community. A lower ratio of phosphorylation Na/H exchanger3/Na/H exchanger3 (pNHE3/NHE3) and a higher sodium monocarboxylate1 (sMCT1) expression were found in the treatment group than in the model group, which may be related to the changes in the mucosal microbial community. Also, the treatments may restore the impacted metabolic pathways of gut microbiota. These results provide an important foundation for mechanism of SL action and developing PL-based treatment for lactose-induced diarrhea.