Toward improving the selective adsorption performance of molecularly imprinted polymers in strong polar solvents, in this work, a new ionic liquid functional monomer, 1‐butyl‐3‐vinylimidazolium bromide, was used to synthesize sulfamethoxazole imprinted polymer in methanol. The resulting molecularly imprinted polymer was characterized by Fourier transform infrared spectra and scanning electron microscopy, and the rebinding mechanism of the molecularly imprinted polymer for sulfonamides was studied. A static equilibrium experiment revealed that the as‐obtained molecularly imprinted polymer had higher molecular recognition for sulfonamides (e.g., sulfamethoxazole, sulfamonomethoxine, and sulfadiazine) in methanol; however, its adsorption of interferent (e.g., diphenylamine, metronidazole, 2,4‐dichlorophenol, and m‐dihydroxybenzene) was quite low. 1H NMR spectroscopy indicated that the excellent recognition performance of the imprinted polymer was based primarily on hydrogen bond, electrostatic and π‐π interactions. Furthermore, the molecularly imprinted polymer can be employed as a solid phase extraction sorbent to effectively extract sulfamethoxazole from a mixed solution. Combined with high‐performance liquid chromatography analysis, a valid molecularly imprinted polymer‐solid phase extraction protocol was established for extraction and detection of trace sulfamethoxazole in spiked soil and sediment samples, and with a recovery that ranged from 93–107%, and a relative standard deviation of lower than 9.7%.