Fix bugs in redirects before request cycle starts and simplify logic.