簡體   English   中英

使用 PHP 創建 XML 站點地圖

[英]Creating an XML sitemap with PHP

我正在嘗試創建一個會自動更新的站點地圖。 我已經做了一些與我的 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>

問題是動態網址(例如從數據庫中提取的網址)沒有生成,站點地圖也不會驗證。 謝謝!

編輯:現在,我只是想讓代碼本身正常工作。 我將它設置為本地測試服務器上的 PHP 文件。 上面的代碼正在被使用。 現在,屏幕或源中沒有任何顯示。 我想我犯了一個語法錯誤,但我找不到任何東西。 任何和所有的幫助表示贊賞!

編輯 2:好的,我把它整理好了。 顯然,我不得不用 PHP 回應 xml 聲明。 最后的代碼貼在上面。 謝謝你的幫助!

如果您查看生成的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 代碼


這可能是因為您的網絡服務器沒有將.xml識別為應該包含 PHP 代碼的文件的擴展名

至少有兩種可能的解決方案:

  • 重新配置您的服務器,以便 XML 文件通過 PHP 解釋器(可能不是一個好主意:這可能會導致現有文件出現問題!)
  • 將站點地圖的擴展名更改為sitemap.php ,例如,它由您的服務器解釋。


我會添加另一個解決方案:

  • 有一個包含代碼的sitemap.php文件
  • 使用 RewriteRule使sitemap.xml URL 實際上指向sitemap.php文件

有了它,您將擁有sitemap.xml URL,這很好(必需?) ,但由於代碼將在sitemap.php ,因此它會被解釋。

請參閱Apache 的mod_rewrite

最簡單的解決方案是在RewriteEngine On之后添加以下行到您的 apache .htaccess文件

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

我已經使用了威廉的代碼(謝謝),並進行了一些小的修改,它對我有用。

我認為這一行:

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

應該是頂部<?php之后的第二行

順便說一句,對復制它的其他人來說只是一個小點,但是在第一行的<?php之前有一個空格字符 - 如果你像我一樣不小心復制它,你會花一些時間試圖弄清楚為什么代碼對你不起作用!

我也不得不稍微調整一下 MySql select 語句。

最后,在輸出中,我使用了一個變量 $domain 以便這段代碼可以用作模板而無需考慮(前提是您每次都使用相同的表名)。 變量被添加到 connectdb.php 文件中,該文件包含在連接數據庫中。

這是我的威廉代碼的工作版本:

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

這是創建和更新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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM