The rapid development of lithium (Li)-ion and sodium (Na)-ion batteries requires advanced solid electrolytes that possess both favorable electrochemical performance and safety assurance. Herein we report a hierarchical poly (ionic liquid)-based solid electrolyte (HPILSE) for high-safety Li-ion and Na-ion batteries. This hybrid solid electrolyte is fabricated via in situ polymerizing 1,4-bis[3-(2-acryloyloxyethyl)imidazolium-1-yl]butane bis[bis(trifluoromethanesulfonyl)imide] (C1-4TFSI) monomer in 1-ethyl-3-methylimidazolium bis(trifluoromethanesulfonyl)imide (EMITFSI)-based electrolyte which is filled in poly(diallyldimethylammonium) bis(trifluoromethanesulfonyl)imide (PDDATFSI) porous membrane. The well-designed hierarchical structure simultaneously provides the prepared HPILSE with high ionic conductivity (>10−3 Scm−1 at 25°C), satisfied electrochemical stability, inherent incombustibility, good mechanical strength and flexibility. More intriguingly, the in situ assembled LiFePO4/Li and Na0.9[Cu0.22Fe0.30Mn0.48]O2/Na cells using HPILSE exhibit superior cycling performances with high specific capacities. Both the excellent performance of HPILSE and the simple fabricating process of HPILSE-based solid-state cells make it potentially as one of the most promising electrolyte materials for next generation Li-ion and Na-ion batteries.