I have done the programming and it works when I call index from another program using CURL and spoofing the user agent, so the logic works OK.
It is just that the real bot (for example sogou.com does not seem to trigger the running of the program, so it appears like it does not follow the header(.... statement like my curl program does. So maybe re-directs are not handled? (that maybe the answer?)
The thing is, I had to program the curl program to handle re-directs before it would run effectively.
I guess that bots are just looking at the text of the one page and not actually following links and re-directs like a browser would do.
Thats a shame as I was hoping to automate something without having to set up a cron job.