One way to go about this (if you are using object oriented programming) is you have more than one phase of your website.
For example, the most basic component can be 'Page', and every page uses this class.
Then you add objects to the page etc etc.
But the main point being, each and every object has some methods:
OnPreRender, OnRender, OnPostRender
Just as an example.
So, when a page request is done, OnPreRender is executed for every object, then on render (which is where you do the echo statements etc and write back data), and then after thats done, do onpostrender.
So, you can do all the hard work in onprerender, and then set a flag to say your redirecting - then in your main page class, check the flag, dont do onrender for the child objects and instead redirect.
Basically, have a 'prepare' phase of your page, and then run the actual page rendering code.