Generating a sitemap with PHP for SEO


I believe this is the code to generate a sitemap on my website (I hope!) and I name it as sitemap.php,


    $xml = "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>"."\
	$xml .= "<urlset xmlns:xsi=\\"\\" xsi:schemaLocation=\\"\\" xmlns=\\"\\">"."\
    $sql = "
		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("&","&amp;",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>"."\
		$xml .= "<loc>$pg_title</loc>"."\
        $xml .= "<lastmod>$pg_date</lastmod>"."\
        $xml .= "<priority>0.50</priority>"."\
		$xml .= "<changefreq>weekly</changefreq>"."\
        $xml .= "</url>"."\

    $xml .= "</urlset>";

    echo $xml;

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.


If your are on an apache server with mod_rewrite, you can create a .htaccess file with the following contents:

RewriteEngine On
RewriteRule ^/?sitemap.xml /sitemap.php

This file goes in your public_html folder.

Now you can call, which will return the output of :slight_smile:

You don’t need to name it sitemap.xml. It can be .php as long as you send a Content-Type header.

Off Topic:

Ok, I’m really sick of the Sitepoint message box. Every time I copy-paste code it also copies the colors and puts everything in a sub-box. I’m not going to bother helping people with their code from now on, because I’m going in a huff like a 10 year old. Bye.

One way is to write the xml output directly to a file: but if so - don’t run this script from a publicly web accessible directory. Someone could in theory hi-jack the script to write a file with their own contents onto your web server in order to gain access to the machine and use it as a spam server and many other nasty things.

If I were you I’d simply print the xml output to the screen, copy it, paste it, save as xml, and upload it. Takes a bit longer but it’s safer and less code :slight_smile: If you only need to run this on a couple sites lazy is good. If you’re going to run this on hundreds of sites then I guess you would want to have the file written via php.

Off Topic:

That’s what the “quote” buttons (down right of the posts) are for :slight_smile:

thank you for this!

I think I am abit slow but can I ask this - does it mean that I don’t need to use robots.txt??


ah got it! thank you… :stuck_out_tongue:

Off Topic:

Ha! Or, you could click the ‘Switch Editor Mode’ button to the top right to prevent this. :wink:

You guys are heroes!! Thank you! :smiley:

No, no…

Loving your posts, keep em coming. :slight_smile:

cool thanks. i will keep it with the .htaccess file

thank you :slight_smile:

robots.txt is something else entirely; you still need it