Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an XML sitemap with PHP

Tags:

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">      <url>         <loc>http://designdeluge.com/</loc>         <lastmod>2010-04-20</lastmod>         <changefreq>weekly</changefreq>         <priority>1.00</priority>     </url>      <url>         <loc>http://designdeluge.com/about.php</loc>         <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."</loc>         <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!

like image 385
williamg Avatar asked Apr 30 '10 21:04

williamg


People also ask

What is PHP sitemap?

In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.

What is XML sitemap explain with example?

XML sitemaps provide search engines with an efficient list of the URLs on a site. XML sitemaps are just text files marked up with tags that identify types of data. The URL for an XML sitemap is typically at the root of a domain — for example, www.example.com/sitemap.xml — ready for bots to access.


2 Answers

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.

like image 143
Pascal MARTIN Avatar answered Sep 21 '22 02:09

Pascal MARTIN


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"?>' ?> 
like image 30
João Pimentel Ferreira Avatar answered Sep 22 '22 02:09

João Pimentel Ferreira