This study investigates the influence of environmental factors on the secondary metabolites of Stachys lavandulifolia Vahl., focusing on how soil properties, temperature, and precipitation affect the yield and chemical composition of its essential oils. The research was conducted in two domains within three rangelands in Mazandaran province, Iran. Data were analyzed using SPSS software. The findings indicated significant variations in essential oil yield across the southern domains of all three study areas. Variance analysis revealed that the interaction between area and domain significantly influenced most of the essential oil compositions, with exceptions for alpha-copaene, alpha-thujene, spathulenol, germacrene-D, hexadecanoic acid, and cis-farnesol. The mean comparison demonstrated substantial differences in the compositions of alpha-pinene, alpha-thujene, myrcene, phytol, and bicyclogermacrene between the northern and southern domains. Furthermore, correlation analysis identified significant relationships between essential oil compositions and various soil factors, such as moisture content, soil stability index, bulk density, phosphorus (P), potassium (K), calcium (Ca), calcium oxide (CaO), magnesium (Mg), sodium (Na), nitrogen (N), sodium adsorption ratio, silt, clay, and organic matter. However, due to the favorable habitat conditions, no significant correlations were observed between temperature, rainfall, and the essential oil compositions in these areas.