In this work, Friedel-Crafts alkylation was successfully applied to prepare a magnetic ionic liquid hypercrosslinked polymer composite (Fe3O4@IL-HCP), which was subsequently employed as magnetic solid-phase extraction (MSPE) adsorbent for the isolation and enrichment of trace non-steroidal anti-inflammatory drugs (NSAIDs). The developed composite was comprehensively characterized using various techniques, with the results indicating that it possessed high saturation magnetization (39.44 em g − 1), large specific surface area (175 m2 g − 1), and high adsorption capacity for NSAIDs. The adsorption behavior and mechanisms were also investigated in detail. NSAIDs were adsorbed onto the Fe3O4@IL-HCP sorbent via a heterogeneous multilayer process consisting of hydrogen bonding and π-π and electrostatic interactions. Additionally, the composite's large surface area and multiple active sites enabled extraction equilibrium within 6 min. By coupling with high performance liquid chromatography (HPLC), the developed MSPE/HPLC method was applied for the determination of selected NSAIDs in water and urine samples. The developed method displayed wide linear ranges, low limits of detection (0.12–0.30 ng mL−1 and 0.15–1.5 ng mL−1 in water and urine samples, respectively), sufficient recoveries (92.8–109%), and good precision (relative standard deviations ≤ 4.6%). Thus, the findings of this work provide an appealing alternative for the extraction and determination of trace NSAIDs in environmental water and biological samples.