首页 > 解决方案 > 已解决:在 sitemap.xml 和 rss.xml 中显示随机标签

问题描述

我的在线 RSS 和站点地图文件中出现了一个随机<head/>标签。它不在我的代码中。

我从头开始使用 PHP/SQL 构建了一个站点,并且正在尝试使站点地图和 rss 文件正确格式化。我正在使用 PHP 生成代码并使用 htaccess 将 sitemap.php 和 rss.php 重定向到 sitemap.xml 和 rss.xml。

我正在使用https://www.sitemaps.org/protocol.html进行格式化。

在线文件在这里(查看页面源以查看<head/>之前的流浪<urlset..)https ://www.cognitame.com/sitemap.xml https://www.cognitame.com/rss.xml

站点地图.php


echo '<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

require('../db_connect.php');
$q = 'SELECT published_time FROM posts ORDER BY published_time DESC LIMIT 1';
$r = mysqli_query($dbc, $q);
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { 
    $published_time = $row['published_time']; }

// Homepage, modified time is when last blog was published
echo '<url>
    <loc>https://cognitame.com/</loc>
    <lastmod>'. $published_time . '</lastmod>
</url>';

// Loop through pages, list them
$q = 'SELECT link, modified_time FROM pages ORDER BY modified_time DESC';
$r = mysqli_query($dbc, $q);
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    echo '<url>
        <loc>' . $row['link'] . '</loc>
        <lastmod>' . $row['modified_time'] . '</lastmod>
    </url>';
}

// Loop through all posts, list them
$q = 'SELECT link, modified_time FROM posts ORDER BY modified_time DESC';
$r = mysqli_query($dbc, $q);
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    echo '<url>
        <loc>' . $row['link'] . '</loc>
        <lastmod>' . $row['modified_time'] . '</lastmod>
    </url>';
}

echo '</urlset>';

?>

RSS.php

    echo '<?xml version="1.0"?>
    <rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">

    <channel>
    <title>Cognitame</title>
    <link>https://cognitame.com</link>
    <description>A personal coding and web development playground.</description>
    <language>en-us</language>';

    // Loop through all posts to print out RSS feed
    require('../db_connect.php');
    $q = 'SELECT post_id, title, link, description FROM posts ORDER BY post_id
        ASC';
    $r = mysqli_query($dbc, $q);

    while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

        echo'<item>
            <title>' . $row['title'] . '</title>
            <link>' . $row['link'] . '</link>
            <description>' . $row['description'] . '</description>
        </item>';
    }

    echo '</channel>
    </rss>';
?>

我期望的最终输出是这样的格式: https://www.sitemaps.org/sitemap.xml

标签: phpxmlrss

解决方案


推荐阅读