Simple Machines Community Forum

Archived Boards and Threads... => Archived Boards => SMF Feedback and Discussion => Topic started by: Stüldt Håjt on January 05, 2007, 08:37:22 AM

Title: Stüldt Håjt's sitemap for Google and Yahoo!
Post by: Stüldt Håjt on January 05, 2007, 08:37:22 AM
I made myself a sitemap for Yahoo! and Google and thought about sharing it with you. Actually two different sitemap versions, one for basic forum and one for mobile searches.

<?php
require_once('SSI.php');
header('Content-Type: application/xml');

echo 
'<?xml version="1.0" encoding="UTF-8"?' '>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
;

echo 
'
<url>
<loc>'
$scripturl'</loc>
<lastmod>'
last_post_time(time()), '</lastmod>
</url>'
;

$request db_query("
SELECT b.ID_BOARD, posterTime
FROM 
{$db_prefix}boards AS b, {$db_prefix}messages
WHERE memberGroups LIKE '%-1%' AND b.ID_LAST_MSG = ID_MSG
ORDER BY posterTime DESC"
__FILE____LINE__);

while (
$row mysql_fetch_array($request)){
$board_id $row['ID_BOARD'];
$time $row['posterTime'];

if (
$modSettings['queryless_urls']==0){

echo '
<url>
<loc>'
$scripturl'?board='$board_id'.0</loc>
<lastmod>'
last_post_time($time), '</lastmod>
</url>'
;
}

else {

echo '
<url>
<loc>'
$scripturl'/board,'$board_id'.0.html</loc>
<lastmod>'
last_post_time($time), '</lastmod>
</url>'
;
}
}

$request db_query("
SELECT t.ID_TOPIC, posterTime, numReplies
FROM 
{$db_prefix}boards AS b, {$db_prefix}messages, {$db_prefix}topics AS t
WHERE memberGroups LIKE '%-1%' AND t.ID_LAST_MSG = ID_MSG AND b.ID_BOARD = t.ID_BOARD
ORDER BY ID_TOPIC DESC
LIMIT 49000"
__FILE____LINE__);

while (
$row mysql_fetch_array($request)){
$topic_id $row['ID_TOPIC'];
$time $row['posterTime'];
$replies $row['numReplies'];

for(
$page_no 0$page_no <= $replies$page_no += $modSettings['defaultMaxMessages']) {

if (
$modSettings['queryless_urls']==0){

echo '
<url>
<loc>'
$scripturl'?topic='$topic_id'.'$page_no'</loc>
<lastmod>'
last_post_time($time), '</lastmod>
</url>'
;
}

else {

echo '
<url>
<loc>'
$scripturl'/topic,'$topic_id'.'$page_no'.html</loc>
<lastmod>'
last_post_time($time), '</lastmod>
</url>'
;
}

}}

echo 
'
</urlset>'
;

function 
last_post_time($int_date) {
$iso_8601_date date('Y-m-d\TH:i:s'$int_date);
$pre_timezone date('O'$int_date);
$time_zone substr($pre_timezone03).":".substr($pre_timezone32);
$iso_8601_date .= $time_zone;
return $iso_8601_date;
}
?>


First it gives your forum's index url then board urls and topic urls. Topic urls includes all pages and are sorted by topic ids newest at top. it also gives the time of last post in each topic. This is suitable for not too big boards. For example my board has over 300k messages and sitemap includes just under 7000 urls and is under 1 MB of size. Sitemap is limited to include 50000 urls and be under 10 MB of size. If those conditions are not met you shoud edit LIMIT 49000 line to make it fit.

This is perfect for those who haven't had sitemaps before and want to give all urls to search engines. Give search engines enough time to crawl these urls and then edit sitemap to feed them newest topics by just editing LIMIT 49000 to 100 for example. Or leave it as it is and make a new sitemap with name sitemap-latest etc.

Add to an Apache config file this line: AddType application/x-httpd-php .php .xml. The line might already be there so search for it. Now save the above code to sitemap.xml and it is ready to be submitted to Google and Yahoo.
http://www.google.com/webmasters/sitemaps/
https://siteexplorer.search.yahoo.com/

If you don't have access to Apache configs you can do rewrite from php to xml with .htaccess-file. If this is the case save above code to sitemap.php. I'm not sure if this works but it's something like:

RewriteEngine on
Options +FollowSymlinks
RewriteRule ^sitemap.xml$ /sitemap.php [NC][L]


Google also accepts mobile sitemaps so I have it also:

<?php
require_once('SSI.php');
header('Content-Type: application/xml');

echo 
'<?xml version="1.0" encoding="UTF-8"?' '>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
;

echo 
'
<url>
<loc>'
$scripturl'?;wap</loc>
<lastmod>'
last_post_time(time()), '</lastmod>
</url>'
;

$request db_query("
SELECT b.ID_BOARD, posterTime
FROM 
{$db_prefix}boards AS b, {$db_prefix}messages
WHERE memberGroups LIKE '%-1%' AND b.ID_LAST_MSG = ID_MSG
ORDER BY posterTime DESC"
__FILE____LINE__);

while (
$row mysql_fetch_array($request)){
$board_id $row['ID_BOARD'];
$time $row['posterTime'];

echo '
<url>
<loc>'
$scripturl'?board='$board_id'.0;wap</loc>
<lastmod>'
last_post_time($time), '</lastmod>
</url>'
;
}

$request db_query("
SELECT t.ID_TOPIC, posterTime
FROM 
{$db_prefix}boards AS b, {$db_prefix}messages, {$db_prefix}topics AS t
WHERE memberGroups LIKE '%-1%' AND t.ID_LAST_MSG = ID_MSG AND b.ID_BOARD = t.ID_BOARD
ORDER BY ID_TOPIC DESC
LIMIT 49000"
__FILE____LINE__);

while (
$row mysql_fetch_array($request)){
$topic_id $row['ID_TOPIC'];
$time $row['posterTime'];

echo '
<url>
<loc>'
$scripturl'?topic='$topic_id'.0;wap</loc>
<lastmod>'
last_post_time($time), '</lastmod>
</url>'
;
}

echo 
'
</urlset>'
;

function 
last_post_time($int_date) {
$iso_8601_date date('Y-m-d\TH:i:s'$int_date);
$pre_timezone date('O'$int_date);
$time_zone substr($pre_timezone03).":".substr($pre_timezone32);
$iso_8601_date .= $time_zone;
return $iso_8601_date;
}
?>


Just edit wap to wap2 and/or imode to suit your needs and save them like sitemap-wap.xml.
Title: Re: Stüldt Håjt's sitemap for Google and Yahoo!
Post by: SacmaliK on January 05, 2007, 12:12:53 PM
Thanks @Stüldt Håjt Beautifull work will be useful to the business of everbody
Title: Re: Stüldt Håjt's sitemap for Google and Yahoo!
Post by: stanaca on April 25, 2008, 01:39:54 AM
I want to use the mobile sitemap but i am using pretty url's. how can i display pretty url's in sitemap
Title: Re: Stüldt Håjt's sitemap for Google and Yahoo!
Post by: niko on April 25, 2008, 04:09:54 AM
Quote from: Stüldt Håjt on January 05, 2007, 08:37:22 AM
Add to an Apache config file this line: AddType application/x-httpd-php .php .xml. The line might already be there so search for it. Now save the above code to sitemap.xml and it is ready to be submitted to Google and Yahoo.
http://www.google.com/webmasters/sitemaps/
https://siteexplorer.search.yahoo.com/

If you don't have access to Apache configs you can do rewrite from php to xml with .htaccess-file. If this is the case save above code to sitemap.php. I'm not sure if this works but it's something like:

RewriteEngine on
Options +FollowSymlinks
RewriteRule ^sitemap.xml$ /sitemap.php [NC][L]


Actually second is better since first will process every xml thru PHP which causes some overhead. (Well small but on bigger sites with many static xmls it has bigger effect)