【问题标题】:Are wildcards allowed in sitemap.xml file?sitemap.xml 文件中是否允许使用通配符?
【发布时间】:2010-07-29 22:23:44
【问题描述】:
我有一个包含 100 多个 html 文件的目录的网站。
我希望爬虫爬取该目录的所有 html 文件。
我已经在我的 robots.txt 中添加了以下句子:
Allow /DirName/*.html$
有没有办法将目录中的文件包含在 sitemap.xml 文件中,以便目录中的所有 html 文件都会被抓取?
像这样的:
<url>
<loc>MyWebsiteName/DirName/*.html</loc>
</url>
【问题讨论】:
标签:
sitemap
wildcard
web-crawler
sitemap.xml
【解决方案2】:
不允许使用通配符。如果您在服务器中运行 php,那么您可以列出目录中的所有文件并使用 DirectoryIterator 自动生成 sitemap.xml。
// this is assume you have already a sitemap class.
$sitemap = new Sitemap;
// iterate the directory
foreach(new DirectoryIterator('/MyWebsiteName/DirName') as $directoryItem)
{
// Filter the item
if(!$directoryItem->isFile()) continue;
// New basic sitemap.
$url = new Sitemap_URL;
// Set arguments.
$url->set_loc(sprintf('/DirName/%1$s', $directoryItem->getBasename()))
->set_last_mod(1276800492)
->set_change_frequency('daily')
->set_priority(1);
// Add it to sitemap.
$sitemap->add($url);
}
// Render the output.
$response = $sitemap->render();
// Cache the output for 24 hours.
$cache->set('sitemap', $response, 86400);
// Output the sitemap.
echo $response;