Efficient recovery of rare earth elements (REEs) from wastewater is crucial for environmental remediation and the sustainable development of resources. Nevertheless, achieving efficient and selective REEs extraction in the systems containing low-concentration REEs and high-concentration competing metal ions remains a challenge. Here, we report on the fabrication of two-dimensional defective carbon nitride (DGCN-K) with extended nanoporous furnished oxygen-enriched sites, which is achieved through re-arrangement of framework structural unit using a flexible polycondensation-ion-thermal strategy. The extended nanoporous and continuous transmission nanochannels establish a dedicated structure microenvironment surrounding the active sites center, promoting the effective ion diffusion and increasing the accessibility of active sites. Meanwhile, the incorporation of abundant electronegative cyano and hydroxyl group modifies coordination microenvironment, improving hydrophilicity and strengthening interaction affinity with REEs. Consequently, DGCN-K shows excellent REEs adsorption performance, achieving high adsorption capacity (Nd(III) 146.25 mg/g, Dy(III) 180.82 mg/g, Lu(III) 204.34 mg/g,) within short adsorption equilibrium time (30 min). Furthermore, DGCN-K possesses high REEs selective removal rate in the mixed binary system and actual leaching tailings. This study provides a feasible microenvironment regulation strategy for the construction of 2D defective carbon nitride with oxygen-enriched sites and extended nanoporous for synergistically enabling efficient recovery of REEs.