php - 已解决:在 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
解决方案
推荐阅读
- raspberry-pi - 与带有树莓派的 Xbee pro-S2C 接口以设置频率参数
- java - 空指针访问:这个变量 dt_str 只能为空
- xml - XSL Modify element value from another element
- android - 设备策略管理器无法在 Android 上设置重置密码令牌(抛出“当前用户禁用托管令牌”异常)
- javascript - 期望断言库方法不起作用
- c++ - 从 exe 和 dll 访问静态库中的外部变量
- excel - Excel中的条件格式交替行和匹配行
- css - 在 Angularjs 上进行验证的 Textarea 无法在 Internet Explorer 11 AngularJS 中工作
- c - C中的范围和条件#define
- xml - XML:Twig 在文件上执行多个 twig 处理程序