Page 1 of 1

Generating a sitemap with PHP for SEO

Posted: Mon Mar 08, 2010 12:42 pm
by lauthiamkok
hi,

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.xml
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

Re: Generating a sitemap with PHP for SEO

Posted: Mon Mar 08, 2010 1:58 pm
by badben
I think that there are three options depending on how often your site is updated.

The first is use the php script to generate an xml file using fopen, fwrite. So rather than outputting the script to the browser use it to generate a seperate xml file. When you update your content you can run sitemap.php and this would create a new file called sitemap.xml, or if it already exists would overwrite sitemap.xml.

The second (and easiest and safest) is to use apache's url_rewrite so you would use:

RewriteRule ^sitemap.xml$ sitemap.php [L]

The third, more dangerous option is to allow xml files to be parsed as php using .htaccess

AddType application/x-httpd-php .php .xml

Re: Generating a sitemap with PHP for SEO

Posted: Mon Mar 08, 2010 2:04 pm
by lauthiamkok
badben wrote:I think that there are three options depending on how often your site is updated.

The first is use the php script to generate an xml file using fopen, fwrite. So rather than outputting the script to the browser use it to generate a seperate xml file. When you update your content you can run sitemap.php and this would create a new file called sitemap.xml, or if it already exists would overwrite sitemap.xml.

The second (and easiest and safest) is to use apache's url_rewrite so you would use:

RewriteRule ^sitemap.xml$ sitemap.php [L]

The third, more dangerous option is to allow xml files to be parsed as php using .htaccess

AddType application/x-httpd-php .php .xml
thank you for this! I think I will use this,

Code: Select all

RewriteRule ^sitemap.xml$ sitemap.php [L]
many thanks!
:mrgreen:

Re: Generating a sitemap with PHP for SEO

Posted: Mon Mar 08, 2010 2:13 pm
by AbraCadaver
Have you tried this:

Code: Select all

Sitemap: http://www.lauthiamkok.net/sitemap.php

Re: Generating a sitemap with PHP for SEO

Posted: Mon Mar 08, 2010 2:18 pm
by lauthiamkok
AbraCadaver wrote:Have you tried this:

Code: Select all

Sitemap: http://www.lauthiamkok.net/sitemap.php
no I haven't. I have it like this instead,

Code: Select all

Sitemap: http://www.lauthiamkok.net/sitemap.xml
I assume that I just need to change to .php in that robots.txt

Thank you. :D