-1

I want to build a sitemap dynamically only when requested by GoogleBot or BingBot and then save it to sitemap.xml, and when other user request it just show then already created sitemap.xml. How to do this ?

Edit

How to serve sitemap.php as sitemap.xml ?

Community
  • 1
  • 1
Sourav
  • 17,065
  • 35
  • 101
  • 159
  • What is your question about - is it about how to build a site map, or how to detect a search engine bot? – Pekka Feb 19 '12 at 14:39

2 Answers2

2

It should be done via htaccess or Server Side scripting

htaccess: redirect all bots using htaccess apache

PHP: something like

//returns 1 if the user agent is a bot
function is_bot($user_agent)
{
  //if no user agent is supplied then assume it's a bot
  if($user_agent == "")
    return 1;

  //array of bot strings to check for
  $bot_strings = Array(  "google",     "bot",
            "yahoo",     "spider",
            "archiver",   "curl",
            "python",     "nambu",
            "twitt",     "perl",
            "sphere",     "PEAR",
            "java",     "wordpress",
            "radian",     "crawl",
            "yandex",     "eventbox",
            "monitor",   "mechanize",
            "facebookexternal"
          );
  foreach($bot_strings as $bot)
  {
    if(strpos($user_agent,$bot) !== false)
    { return 1; }
  }

  return 0;
}

Atention This is strongly discouraged by Google. I only post this 'cause sometimes it can be useful.

edit It always amazes me the capacity to reinvent the wheel. If anyone wants to generate sitemaps please take a look at: http://code.google.com/p/sitemap-generators/wiki/SitemapGenerators

There's also some paid services.

Community
  • 1
  • 1
jribeiro
  • 3,387
  • 8
  • 43
  • 70
  • I'll add to that by noting the sitemap.xml file is something best generated and stored on the webserver as a static file. Of course PHP could be used to generate it, just not in real time during every visit from the google bot. – quickshiftin Feb 20 '12 at 06:31
1

If you want to save your file as .xml and execute it as a PHP file, you'll need to add something along these lines to your .htaccess file:

AddType x-mapp-php5 .php .xml
BenOfTheNorth
  • 2,904
  • 1
  • 20
  • 46
  • I want to process only some specified XML file as PHP, not all XML file. Is it possible ? – Sourav Feb 23 '12 at 03:38
  • XML files will still work as an XML file too - but if there is any PHP in then, that'll work two. Think of it as a boost to the existing functionality of the XML files you have. – BenOfTheNorth Feb 23 '12 at 11:10