Generating a sitemap with PHP for SEO
Posted: Mon Mar 08, 2010 12:42 pm
hi,
I believe this is the code to generate a sitemap on my website (I hope!) and I name it as sitemap.php,
and I should have a robots.txt file with a line like this,
but the php file should be name with a .php extension like sitemap.php, and the thing I still cannot get my head around is how to execute this sitemap.php so that I will have a sitemap.xml??
many thanks if u have any ideas.
cheers,
Lau
I believe this is the code to generate a sitemap on my website (I hope!) and I name it as sitemap.php,
Code: Select all
<?php
require("incl_core/conn_mysql.inc.php");
require("incl_core/core.php");
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"."\n";
$xml .= "<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"."\n";
$sql = "
SELECT *
FROM root_pages
WHERE root_pages.pg_cat_id = '2'
AND root_pages.pg_hide != '1'
ORDER BY root_pages.pg_updated DESC
LIMIT 10";
$query = mysql_query($sql) or die ("Could not execute query");
while($row = mysql_fetch_array($query)) {
$pg_title = str_replace(" ", "-", $row['pg_title_clean']);
$pg_title = str_replace("&","&",strip_tags($pg_title));
$pg_title = htmlentities($pg_title, ENT_QUOTES, "UTF-8");
$pg_date = $row['pg_updated'];
$pg_date = strtotime($pg_date);
$pg_date = date(DATE_W3C, $pg_date);
$xml .= "<url>"."\n";
$xml .= "<loc>http://www.lauthiamkok.net/portfolio/$pg_title</loc>"."\n";
$xml .= "<lastmod>$pg_date</lastmod>"."\n";
$xml .= "<priority>0.50</priority>"."\n";
$xml .= "<changefreq>weekly</changefreq>"."\n";
$xml .= "</url>"."\n";
}
$xml .= "</urlset>";
echo $xml;
?>and I should have a robots.txt file with a line like this,
Code: Select all
Sitemap: http://www.lauthiamkok.net/sitemap.xmlmany thanks if u have any ideas.
cheers,
Lau