I've been asked to write an SEO book from a technological point of view. As part of the process, I will post some of my ideas on this blog. The title is TBD, but it will be a handbook for all the common questions and concerns that a search engine marketer will have addressed entirely from a programming angle. A stress will be placed on showing examples in PHP, and, perhaps, a later edition in ASP. As a first topic, I will discuss the topic of search engine sitemaps. I am not referring to the general concept of a sitemap, though the origin of the idea comes from the obvious advantages that were noticed when people created a page that functioned as an enumeration of all the URLs on a site. I will show a script that creates both files for a dynamic site with flexibility.

As a side-note. Using a sitemap will probably not get you ranked any higher in the SERPs with any certainty. However, what it definately does do is get your site indexed much quicker. In my experience, Google hits your sitemap a few times a day. This can be an edge in terms of getting into the SERPs faster.

First, we need some simple rewrites in our site's root directory:
RewriteEngine On RewriteRule ^sitemap.xml$ /sitemap.php?id=google [PT]
RewriteRule ^urllist.txt$ /sitemap.php?id=yahoo [PT]

The script "sitemap.php" takes a parameter "id." Here is that script.

<?

require_once('./Sitemap.php');

$s = new Sitemap();

$categories Categories::get();
foreach (
$categories as $c) {
    
$s->addItem(Catalog::makeCategoryLink($c['id']), '''weekly');    
}

$pages Pages::get();
foreach (
$pages as $p) {
    
$s->addItem(Catalog::makePageLink($p['primary_category_id'], $p['id']), DB::unixToSQL(strtotime($p['date_updated'])), 'weekly');
}

$s->addItem('http://www.somedomain.com/Somepage.html''''daily');

if (
$_REQUEST['id'] == 'google') {
    echo 
$s->getGoogle();    
} else if (
$_REQUEST['id'] == 'yahoo'){
    echo 
$s->getYahoo();
}

?> And here is the code for the Sitemap class: <?

class Sitemap
{

    function Sitemap($items = array()) 
    {
        
$this->_items $items;
    }
    
    function 
addItem($url$lastmod ''$changefreq ''$priority ''$additional_fields = array())
    {
        
$this->_items[] = array_merge(array('loc' => $url'lastmod' => $lastmod'changefreq' => $changefreq'priority' => $priority), $additional_fields);
    }
    
    function 
getGoogle()
    {
        
ob_start();
        
header('Content-type: text/xml');
        echo 
'<?xml version="1.0" encoding="UTF-8"?>';
        
?>
        <urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
        <? foreach ($this->_items as $i): ?>
            <url>
            <?
            
foreach ($i as $index => $_i) {
                if (!
$_i) continue;
                echo 
"<$index>" Format::escapeXML($_i) . "</$index>";
            }
            
?>
            </url>            
        <? endforeach; ?>        
        </urlset>
          <?
        
return ob_get_clean();    
    }    
        
    function 
getYahoo()
    {
        
ob_start();
        
header('Content-type: text/plain');
        foreach (
$this->_items as $i) {
            echo 
$i['url'] . "\n";
        }
        return 
ob_get_clean();    
    }
    
}

?>

Tell an amigo:
  • Sphinn
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Facebook



Related posts:
RSS Made Even Simpler Syndication isn't only for blogs; it can be used for...
Yahoo API Code <?php /* sample usage: $tree = new xmlTreeParser('sample.xml'); echo '<pre>'; print_r($tree->getTree()); */ class xmlTreeParser {     var $_parser;     var $_xmldata;...
Professional SEO with PHP Book by Jaimie Sirovich and Cristian Darie, Wrox, April 2007 The...
Stop SEO Death By Pagination This post is now (mostly) obsolete and replaced by WordPress...
SEO For Book Titles? Or is it search engine optimization?  It occurs to me...