开发者

Creating an XML sitemap with PHP

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</changefre开发者_高级运维q>
        <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!


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.


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"?>' ?>


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>


Here is the easiest way to creating and updating sitemap.xml file.

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

require_once('database.php');

$sitemapText = '<?xml version="1.0" encoding="UTF-8"?>
    <urlset
          xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
          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">
    <url>
      <loc>http://ajkhub.com/</loc>
      <lastmod>2021-08-18T18:32:09+00:00</lastmod>
      <priority>1.00</priority>
    </url>
    <url>
      <loc>http://ajkhub.com/includes/about.php</loc>
      <lastmod>2021-08-18T18:32:09+00:00</lastmod>
      <priority>0.80</priority>
    </url>
    <url>
      <loc>http://ajkhub.com/includes/privacy-policy.php</loc>
      <lastmod>2021-08-18T18:32:09+00:00</lastmod>
      <priority>0.80</priority>
    </url>
    <url>
      <loc>http://ajkhub.com/includes/termsandcondition.php</loc>
      <lastmod>2021-08-18T18:32:09+00:00</lastmod>
      <priority>0.80</priority>
    </url>';

$sql = "SELECT * FROM page ORDER BY id DESC LIMIT 4";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
   while($row = mysqli_fetch_assoc($result)) {
$sitemapText .= ' <url>
                 <loc>'.$actual_link."/".$row['page'].'</loc>
                 <lastmod>'.date(DATE_ATOM,time()).'</lastmod>
                 <priority>0.80</priority>
               </url>';
   }
}

$sitemapText .= '</urlset>';

$sitemap = fopen("sitemap.xml", "w") or die("Unable to open file!");

fwrite($sitemap, $sitemapText);
fclose($sitemap);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜