tags:

views:

269

answers:

1

I'm trying to create a sitemap that will automatically update. I've done something similiar with my RSS feed, but this sitemap refuses to work. You can view it live at http://designdeluge.com/sitemap.xml I think the main problem is that its not recognizing the PHP code. Here's the full source:

 <?php 


include 'includes/connection.php';

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

echo '<?xml version="1.0" encoding="UTF-8" ?>';

?>

<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"&gt;

    <url>
        <loc>http://designdeluge.com/&lt;/loc&gt;
        <lastmod>2010-04-20</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.00</priority>
    </url>

    <url>
        <loc>http://designdeluge.com/about.php&lt;/loc&gt;
        <lastmod>2010-04-20</lastmod>
        <changefreq>never</changefreq>
        <priority>0.5</priority>
    </url>

    <?php

    $entries = mysql_query("SELECT * FROM Entries");

    while($row = mysql_fetch_assoc($entries)) {
    $title = stripslashes($row['title']);
    $date = date("Y-m-d", strtotime($row['timestamp']));

    echo "

    <url>
        <loc>http://designdeluge.com/".$title."&lt;/loc&gt;
        <lastmod>".$date."</lastmod>
        <changefreq>never</changefreq>
        <priority>0.8</priority>
    </url>";

 } ?>

</urlset>

The problem is that the dynamic URL's (e.g. the ones pulled from the DB) aren't being generated and the sitemap won't validate. Thanks!

EDIT: Right now, I'm just trying to get the code itself working. I have it set up as a PHP file on my local testing server. The code above is being used. Right now, nothing displays nothing on screen or in the source. I'm thinking I made a syntax error, but I can't find anything. Any and all help is appreciated!

EDIT 2: Ok, I got it sorted out guys. Apparently, I had to echo the xml declaration with PHP. The final code is posted above. Thanks for your help!

+3  A: 

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.

Pascal MARTIN
Well the only two .xml files on my server are using php so I don't see any harm. Would doing the first option be something like adding this to a .htaccess file? AddType application/x-httpd-php .xml
WillyG
Not sure this can be done in a `.htaccess` file *(depending ohn your server's configuration, it might be necessary to do it directly in the configuration of the server)* ;; but you could try, maybe it'll work :-)
Pascal MARTIN
@iMaster: For your code to work, short_tags must also be off (it is by default--just something to be aware of). Additionally, when I currently visit http://designdeluge.com/sitemap.xml I get an HTTP 500 response, so your config is broken (probably 'cause you're working on it right now). Lastly, for large sites, generating the whole sitemap in realtime is probably infeasible -- so if you're expecting to grow to thousands of pages or more, be prepared to reimplement this later.
Frank Farmer
I'm not sure what's causing that 500 Error...all I added to my .htaccess file are the two rewrite rules, but I don't think that's causing it. I added this: RewriteRule ^rss\.xml$ rss.phpRewriteRule ^sitemap\.xml$ sitemap.php [L]Not sure why that's causing an error... Is there any way to get more info on what's causing an error like that?
WillyG