Fixes #5238: More reliably redirect URLs with /pg/

This replaces #5238. Biggest different from master is that the redirect has to happen *after* start.php. I think it's worth it to redirect more accurately though.