Changes in the gut microbiota during pathogen infection are often predicted to influence disease outcomes. However, studies exploring whether pathogens induce microbiota shifts have yielded inconsistent results. This suggests that variation in infection, rather than the presence of infection alone, might shape pathogen-microbiota relationships. For example, most hosts are coinfected with multiple pathogens simultaneously, and hosts vary in how long they are infected, which may amplify or diminish microbial shifts expected in response to a focal pathogen. We used a longitudinal anthelmintic treatment study of free-ranging African buffalo (Syncerus caffer) to examine whether (i) coinfection with bovine tuberculosis (Mycobacterium bovis, TB) and gastrointestinal nematodes, and (ii) the duration of TB infection, modified effects of single pathogens on the gut microbiota. By accounting for the interaction between TB and nematodes, we found that coinfection affected changes in microbial abundance associated with single infections. Furthermore, the duration of TB infection predicted more microbiota variation than the presence of TB. Importantly, coinfection and infection duration had nearly as much influence on microbial patterns as demographic and environmental factors commonly examined in microbiota research. These findings demonstrate that acknowledging infection heterogeneities may be crucial to understanding relationships between pathogens and the gut microbiota.