I have a sitemap file which generates the sitemap in real time and lists every page on the site. Eventually the site will have tens of thousands of pages so generating the sitemap will take a fair bit of resources to do so I don't want just anyone to access the sitemap. What I am trying to achieve is something similar to the way Stack Exchange does it where search engines requesting the sitemap will be able to access it properly but if a person tries to access the sitemap file directly they will be served with a 404 not found error.
Could anyone point me in the right direction for how to do this. I already have a rewrite rule in place to rewrite sitemap.xml to sitemap.php for the generating script so now just need to limit access to the file. I would prefer to do this through a .htaccess file or through the vhost file but if it has to be done in the PHP so be it.