Creating an XML sitemap with PHP

The simplest solution is to add to your apache .htaccess file the following line after RewriteEngine On

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

and then simply having a file sitemap.php in your root folder that would be normally accessible via http://yoursite.com/sitemap.xml, the default URL where all search engines will firstly search.

The file sitemap.php shall start with

<?php header('Content-type: application/xml; charset=utf-8') ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>' ?>

If you take a look at the sitemap.xml that's generated (using view source, in your browser, for example), you'll see this :

<?php header('Content-type: text/xml'); ?>
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http:/
...

The <?php, present in that output, shows that PHP code is not interpreted.


This is probably because your webserver doesn't recognize .xml as an extension of files that should contain PHP code.

At least two possible solutions :

  • Re-configure your server, so XML files go through the PHP interpreter (might not be such a good idea : that can cause problems with existing files ! )
  • Change the extension of your sitemap, to sitemap.php for example, so it's interpreted by your server.


I would add another solution :

  • Have a sitemap.php file, that contains the code
  • And use a RewriteRule so the sitemap.xml URL actually points to the sitemap.php file

With that, you'll have the sitemap.xml URL, which is nice (required ? ), but as the code will be in sitemap.php, it'll get interpreted.

See Apache's mod_rewrite.


I've used William's code (thanks) and with a few small modifications it worked for me.

I think the line:

header("Content-type: text/xml");

should be the second line after the top <?php

Incidentally, just a small point to anyone else that copies it, but there is a single space character before the <?php in the first line - if you inadvertantly copy it as I did, you will spend a bit of time trying to figure out why the code won't work for you!

I had to tweak the MySql select statement a little bit too.

Finally, in the output, I have used a variable $domain so that this piece of code can be used as a template without the need to think about it (provided you use the same table name each time). The variabe is added to the connectdb.php file which is included to connect to the database.

Here is my working version of the William's code:

<?php 
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
include 'includes/connectdb.php';
?>

<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">

    <url>
        <loc>http://www.DOMAIN.co.uk/</loc>
        <priority>1.00</priority>
    </url>

    <?php

    $sql = "SELECT * FROM pages WHERE onshow = 1 ORDER BY id ASC";
    $result = mysql_query($sql,$conn);      
    while($row = mysql_fetch_array($result))
    { 
    $filename = stripslashes($row['filename']);
    ?>
    <url>
        <loc>http://www.<?php echo "$domain"; ?>/<?php echo "$filename" ?></loc>
        <changefreq>monthly</changefreq>
        <priority>0.5</priority>
    </url>

 <?php } ?>

</urlset>