Short Rests and Long Rests are integral to the D&D 5e ruleset. Typically measured in 1-hour and 8-hour increments (PHB 186), there is an alternate rule for better grittier games, where a Short Rest is 8 hours long and a Long Rest runs 7 days (DMG 267). Short Rests provide a chance for PCs (and allies) to regain a few spells and some hit points. Long Rests return ALL spells and hit points.
In both cases, rests are presumed to take place in relatively safe areas.
Megadungeons are notoriously unsafe.
After all, the dungeon hates you.
Still, the dungeon prefers to toy with surface invaders, rather than kill them outright. It knows the centuries are long and entertainment should last and be savored. The dungeon understands that in the end, adventurers die. They always die, be it with loved ones in the sunlight or alone in the darkness.
Smart adventurers know that resting in a dungeon is dangerous, and resting in a megadungeon is foolhardy in the extreme. Despite this knowledge, rests are sometimes necessary. This necessity should spring from the PCs being too far from the surface to safely retreat, having either delved too deep or because the PCs are lost in the dungeon.
Traps, PC actions, and the occasional monster can result in the PCs becoming lost. Traps are the most common means of getting lost: chutes, pits, teleporters, disappearing or one-way stairs, one-way elevators, or the various ways to seal the path behind them. PC actions are typically of the 'running blindly from certain death into only possible death' variety, but using explosive spells or items can accidentally drop a bridge or ceiling, collapse walls and floors, flood areas impassably, or create new walls. By occasional monster, I am referring to the Crypt Thing of Fiend Folio fame.
Crypt Thing by Russ Nicholson, Fiend Folio, 1981
The Crypt Thing exists to speak cryptically and to teleport people a random direction and distance away, then tell the survivors that it disintegrated the others, and would they like to be next. Luckily, regardless of which version of D&D you play, the Crypt Thing has already been converted, so you and your players can experience random malicious teleportation regardless of your game.
For the record, the Crypt Thing in my megadungeon is more of a dungeon-sage and has some rather specific locations it teleports victims to, all for the amusement of the dungeon: A large pile of manure near one of Skara Brae's market squares; a similar midden, but elsewhere in the dungeon; above a lake near Skara Brae; above the dark lake on level 10. I may add 2-4 more, just for variety and because I have the appropriate dice.
As we see, PCs can easily find themselves lost and in dire need of hit points and spells, so they choose to rest. So what to do?
One option is to take Jeff's Reints' approach, with a table for what happens to those foolish enough to rest.
But that is a bit much, even for me.
Wise PCs study the maps they have (hopefully) been making to determine possible safe rooms, or take a chance if accurate maps don't exist. Once in a room, the entrance(s) should be secured as best as possible, using iron spikes if nothing else. That done, watches should be established and rest happens. Food is eaten, wine washes it down, wounds are bound, swords sharpened, spells cast and memorized, socks changed, etc. Long Rests are a fine time for some interparty roleplaying if you're into that. If not, move straight to the rolling for wandering monsters.
Yes, my megadungeon has wandering monsters. I'd argue that a good megadungeon should. It could be the dungeon spontaneously creates them to send the PCs' way or perhaps the monsters already exist, and the dungeon merely nudges them in the PCs' direction. But maybe - just maybe - wandering monster encounters truly are the will of Avrae, the goddess of chance.
Fortunately for adventurers with spellcaster sorts among them, several low level spells exist to help with resting in hostile environments: Alarm, Hold Portal, Silent Image, Arcane (or Wizard) Lock, Rope Trick, and Leomund's Tiny Hut. If a spell isn't in your ruleset, find a version and convert it.
Another method is to learn the 'rules' of the megadungeon and its populations. An alliance with one faction or another may provide PCs refuge and assistance, perhaps for a fee, a service, or services rendered. Realizing that nothing goes to a specific area AND removing the threat the keeps locals away buys safety, if only for a time. In theory, sections of a megadungeon level can be cleared, rendering them safe, but nature - and the dungeon - abhor a vacuum, so the cleared areas fill back up.
Establishing forward camps with supplies and maybe permanent guards is dangerous, expensive, and eventually ends badly. But until it does, this provides a fine source of safe rest and replacement PCs from among the guards and apprentices.
Magic items, such as Daern's Instant Fortress, a folding boat, or a ship in a bottle can make a safe haven, unless the Instant Fortress collapses a ceiling upon itself or until one realizes that any body of water in the megadungeon that is deep and large enough for a ship is also deep and large enough for its own inhabitants.
Not my work, google turned it up. If you know the artist, tell me.
Inhabitants that are not above the floating buffet the PCs thoughtfully provided.
Inhabitants that probably have tentacles.
Resting is as natural as working, and just as necessary for PCs as much as the people that play them. Let the PCs rest, but be sure they understand that a good rest is not guaranteed.
Time to give this topic a rest.