Habitat quality and the impact of natural enemies might profoundly affect metapopulation dynamics and viability. However, their relative impact has usually been considered independently. Here we address the question of how caterpillar habitat quality and parasitism prevalence interact to shape habitat selection in the bog fritillary butterfly Boloria eunomia, parasitized at the caterpillar stage by a specialist wasp, Cotesia eunomiae. We first classified habitat quality by relating caterpillar density to descriptors of different microhabitat types. Second, we investigated parasitism prevalence in those different microhabitats. Our results show that caterpillars and parasitoids mapped onto the same microhabitats, mainly zones with high abundance of the host plant. Accordingly, we suggest that both egg-laying females and parasitoids use the same cues for habitat selection. As a consequence, there should be a fitness cost for B. eunomia females to lay their eggs in places where parasitism prevalence is high. We indeed detected that B. eunomia females frequently laid eggs in habitat types that presented suboptimal microhabitat conditions for caterpillars. This suggests that the lower parasitism prevalence in these suboptimal habitat types counterbalances lower caterpillar survival, leading to an overall similar survival in optimal and suboptimal habitat types. Spreading eggs in different habitat types is thus expected to be a safe strategy to mitigate the adverse possible effects of environmental stochasticity and parasitism prevalence on offspring survival unequal among microhabitat types. From a conservation viewpoint, the preservation of habitat heterogeneity, including optimal but also suboptimal habitat areas, is crucial to ensure persistence of both the host and its parasitoid. J. Choutt Á C. Turlure Á N. Schtickzelle (