Heyya NC -
Let's consider a slightly different approach and see if this bakes your noodle. First, let's take all requests for all pages and ship them into a single script, regardless of surfer type. This is done in your .htaccess:
RewriteEngine on
RewriteRule ^(.*)$ /main.php?uri=$1
Now in the main.php, let's evaluate what kind of surfer they are, and pass the correct page back. I'm also going to make use of the dbConnection class in the php repository here (it's my class) because you'll be using your connection to the database more than just for identifying spiders:
<?php
require "class.dbconection2.php";
$db = new dbConnection('127.0.0.1', 'user', 'password', 'database');
// This will return a zero or a 1 - true or false, if it is a spider
$isSpider = $db->singleAnswer("select count('x') from spiders where address='{$_SERVER['REMOTE_ADDR']}'");
if ($isSpider)
{
require "spiderpages/$_GET['uri']";
} else {
require "humanpages/$_GET['uri']";
}
?>
Now for this to work, you will need a directory under your <main.php> called humanpages and a directory called spiderpages. In both, you will create a page called index.html. In both, you will put the content that you want to display of that page is requested, whether the surfer is a spider or a human. Note also that it doesn't matter that the required scripts are .html at the end rather than .php, since you're already executing php the interpreter will not care.
Alternately, consider this code:
<?php
require "class.dbconection2.php";
$db = new dbConnection('127.0.0.1', 'user', 'password', 'database');
// This will return a zero or a 1 - true or false, if it is a spider
$isSpider = $db->singleAnswer("select count('x') from spiders where address='{$_SERVER['REMOTE_ADDR']}'");
$pageScript = $db->singleAnswer("select pagescript from pages where pagename='{$_GET['uri']}' and spider=$isSpider");
echo eval($pageScript);
?>
This very tiny script will decide if the user is a spider, then pull a php script
from the database and then execute the code (the eval line). This is another way of storing all of your pages, but in a database rather than in the file system. Of course this script assumes that (the page required) returns a string that is the output of the page. Another way to do this if pages are all completely static would be:
<?php
require "class.dbconection2.php";
$db = new dbConnection('127.0.0.1', 'user', 'password', 'database');
// This will return a zero or a 1 - true or false, if it is a spider
$isSpider = $db->singleAnswer("select count('x') from spiders where address='{$_SERVER['REMOTE_ADDR']}'");
echo $db->singleAnswer("select pagescript from pages where pagename='{$_GET['uri']}' and spider=$isSpider");
?>
... where we just push back out whatever is in the database with that name and no evaluation of "code" at all... just a straight page dump.
It gets way more complicated and cool from here, but I think this ought to keep you up for a few nights thinking about how you want to cloak. Note that same-server cloaking is way more (WAY more) effective than redirecting, both for spiders and surfers - especially if you are trying to sell them something, so I think you're on the right track.