使用PHP创建XML站点地图

25

我正在尝试创建一个会自动更新的站点地图。 我已经用我的RSS源文件做了类似的事情,但是这个站点地图不起作用。 您可以在http://designdeluge.com/sitemap.xml上实时查看它。 我认为主要问题是它无法识别PHP代码。 这是完整的源代码:

 <?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>
问题在于动态URL(例如从数据库中获取的URL)未被生成,因此网站地图无法验证。谢谢!

编辑:目前,我只是试图使代码本身正常工作。我将其设置为我的本地测试服务器上的PHP文件。上面的代码正在使用。目前,在屏幕上或源代码中没有显示任何内容。我认为我可能犯了语法错误,但我找不到任何错误。感谢您的任何帮助!

编辑2:好了,伙计们,我解决了。显然,我必须用PHP输出XML声明。最终的代码已发布在上面。感谢您的帮助!

可能是PHP代码未被执行,而是在页面上显示的重复问题。 - miken32
你应该在网站地图中使用UTF-8编码的URL。我看不到你用来编码动态生成的URL的任何代码。 - Hasanuzzaman Sattar
4个回答

43

如果你查看生成的sitemap.xml(例如在浏览器中使用查看源代码),你会看到这个:

<?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:/
...

这段输出中出现的<?php表明PHP代码没有被解释


这可能是因为您的Web服务器无法识别.xml作为应包含PHP代码的文件扩展名

至少有两种可能的解决方案:

  • 重新配置服务器,使XML文件通过PHP解释器(可能不是一个好主意:这可能会导致现有文件出现问题!)
  • 更改站点地图的扩展名,例如为sitemap.php,以便由服务器解释。


我想再添加另一个解决方案:

  • 拥有一个包含代码的sitemap.php文件
  • 使用RewriteRule,使sitemap.xml URL实际上指向sitemap.php文件

这样,您将拥有sitemap.xml URL,这很好(必需?),但由于代码将在sitemap.php中,因此它将得到解释。

请参阅Apache的mod_rewrite


我的服务器上只有两个 .xml 文件在使用 php,所以我不认为会有任何问题。采用第一种选项是不是像将以下内容添加到 .htaccess 文件中?AddType application/x-httpd-php .xml - williamg
2
不确定是否可以在 .htaccess 文件中完成此操作 *(取决于您的服务器配置,可能需要直接在服务器配置中完成)*,但是您可以尝试一下,也许会起作用 :-) - Pascal MARTIN
@iMaster:为了让你的代码正常工作,short_tags也必须关闭(默认情况下是关闭的——只是需要注意一下)。此外,当我当前访问http://designdeluge.com/sitemap.xml时,我会收到HTTP 500响应,因此你的配置已经损坏(可能是因为你正在进行工作)。最后,对于大型网站,在实时生成整个站点地图可能是不可行的——因此,如果你预计将增长到数千页或更多,请准备稍后重新实现此功能。 - Frank Farmer
我不确定是什么导致了500错误... 我只在我的.htaccess文件中添加了两个重写规则,但我不认为这是导致问题的原因。我添加了这个:RewriteRule ^rss.xml$ rss.php RewriteRule ^sitemap.xml$ sitemap.php [L]不确定为什么会出现错误...有没有办法获取关于这种错误原因的更多信息? - williamg
这是一个好的 .htaccess 文件示例:http://pingbin.com/2011/03/sitemap-php-to-sitemap-xml-htaccess-file/ - sumitkanoje

11
最简单的解决方案是在您的apache .htaccess文件中,在RewriteEngine On之后添加以下行。
RewriteRule ^sitemap\.xml$ sitemap.php [L]

只需要在您的根目录中放置一个名为sitemap.php的文件,通常可以通过http://yoursite.com/sitemap.xml访问,这是所有搜索引擎首先搜索的默认URL。

sitemap.php文件应该以以下方式开头:

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

9

我使用了William的代码(感谢他),并进行了一些小修改,它对我有用。

我认为这行代码:

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

在顶部 <?php 后应该是第二行。

顺便说一下,对于任何复制此代码的人来说,有一个单个空格字符位于第一行的 <?php 之前 - 如果您不小心像我一样复制它,您将花费一些时间来尝试解决为什么代码不起作用的问题!

我还必须微调MySql选择语句。

最后,在输出中,我使用了一个变量$domain,以便可以将此代码片段用作模板,而无需考虑它(前提是每次都使用相同的表名)。该变量添加到connectdb.php文件中,该文件包含用于连接到数据库的代码。

这是William的代码的可工作版本:

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

1
以下是创建和更新 `sitemap.xml` 文件的最简单方法。
$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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接