我正在使用 php 脚本生成我的网站 sitemap.xml
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
// Define and perform the SQL SELECT query
$sql = "SELECT `title`,`id`,`type`,`updated_at` FROM `titles`";
$result = $conn->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
// Parse the result set, and add the URL in the XML structure
foreach($result as $row) {
$xmlsitemap .= '<url>
<loc>/'. $type1 .'/'. $row['id'] .'-'. $string .'</loc>
<lastmod>'. $date1 .'</lastmod>
<priority>0.5</priority>
<changefreq>weekly</changefreq>
</url>';
}
}
$xmlsitemap .= '</urlset>';
file_put_contents($xmlfile, $xmlsitemap); // saves the sitemap on server
目前,我的 sitemap.xml 文件中大约有 37,000 个网址。
我知道站点地图的网址上限为 50,000 个。在 php 中是否有一种简单的方法告诉脚本在一定数量的行后创建第二个 sitemap2.xml,例如。每个 url 将有大约 6 行代码,所以我估计新文件应该在达到 250,000 行代码时创建?
此外,在您的服务器根目录中包含多个 sitemap.xml 文件的最佳方式是什么,以便所有站点地图文件都将被搜索引擎机器人读取?
最佳答案
您需要将迭代器添加到foreach
。如果迭代器 >=50.000
关于PHP file_put_contents : Create new file after 250, 000行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23970129/