Generating a sitemap with PHP for SEO

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Generating a sitemap with PHP for SEO

Post 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
badben
Forum Newbie
Posts: 4
Joined: Fri Oct 16, 2009 11:57 am
Location: Lancashire, UK

Re: Generating a sitemap with PHP for SEO

Post 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
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: Generating a sitemap with PHP for SEO

Post 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:
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Generating a sitemap with PHP for SEO

Post by AbraCadaver »

Have you tried this:

Code: Select all

Sitemap: http://www.lauthiamkok.net/sitemap.php
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: Generating a sitemap with PHP for SEO

Post 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
Post Reply