好些年前,我們的網站就用PHP程序來動态生成robots.txt和sitemap.xml,其中網站地圖用php來動态生成而沒有保存成文件,因為有些站網址太多,怕sitemap文件過多、過大而占用磁盤空間。雖然動态生成會對數據量很大的數據庫有一定影響,但經過一些優化還是可以承受的。但URL很多的網站還存在另外一個問題,就是sitemap被大量爬取,占用帶寬,這個問題因為不是很普遍、很迫切,所以就一直放着沒有動,隻是把有一個站的php程序中添加了延時來讓爬蟲慢一點。

前幾天在為新改版的郵編庫添加網站地圖的時候,用MediaWiki自帶的maintenance/generateSitemap.php生成地圖文件,發現默認compress參數是yes,生成的網站地圖都是.xml.gz的文件,除了sitemap.xml這個index頁以外,觀察了一下,正常的單個xml文件是10M字節,壓縮後的xml.gz文件大約是200多K字節,隻有以前的幾十分之一。對于數百萬URL,以前近200個xml文件需要上G的空間,現在隻要幾十M,不僅空間占用少得多,爬蟲下載也會快很多倍。
于是想到把以前php生成的sitemap.xml也改為.xml.gz格式,修改以前的sitemap.php中的部分代碼:
......
# 準備輸出内容
$output = '';
if ($query_string == NULL) { //index file or single file, example: https://cidian.18dao.net/sitemap_cidian.xml or https://cidian.18dao.net/sitemap_bihua.xml
$result_count = mysqli_query($link,$sql_count);
while ($row = $result_count->fetch_array()) {
$num_rows = $row[0];
}
$pages = ceil($num_rows / $url_per_page);
if ($pages == 1) {//single file, example: https://cidian.18dao.net/sitemap_bihua.xml
$sql_page = "$sql LIMIT 10000 OFFSET 0";
$result_page = mysqli_query($link,$sql_page);
$output .= map_start();
while ($row = $result_page->fetch_array()) {
$value = $row[0];
if ($value != '') {
if (isset($row[1])) {
$value1 = $row[1];
}
$value_urlencode = urlencode($value);
if (strpos($value,'&') == FALSE) {
$output .= "";
if (isset($row[1]) && !is_date_time($value1)) {
$output .= "https://$http_host/$path/$value_urlencode/".urlencode($row[1])."";
} else {
$output .= "https://$http_host/$path/$value_urlencode";
}
if (isset($row[1]) && is_date_time($value1)) {
$output .= "".str_replace(' ','T',$value1)."Z";
} else {
$output .= "$lastmod";
}
$output .= "$changefreq";
$output .= "$priority";
$output .= "\n";
}
}
}
$output .= map_end();
} else {//index file, example: https://cidian.18dao.net/sitemap_cidian.xml
$output .= index_start();
if (is_date_time($lastmod)) {
$lastmod_index = $lastmod;
} else {
$lastmod_index = $lastmod_default;
}
for ($i=1; $i<=$pages; $i++) {
$output .= "\t\n";
//2021-6-16//$output .= "\t\thttps://$http_host$request_uri?page=$i\n";//2021-6-16
$output .= "\t\thttps://$http_host$request_uri.gz?page=$i\n";//2021-6-16
$output .= "\t\t$lastmod_index\n";
$output .= "\t\n";
}
$output .= index_end();
}
header("Content-Type: application/xml");
print $output;
} else {//paged file, example: https://cidian.18dao.net/sitemap_cidian.xml?page=2
$page = substr($query_string, 5); //example: 2
$offset = ($page - 1) * $url_per_page;
$limit = $url_per_page;
//$sql = "SELECT DISTINCT $field FROM $table WHERE not $field like '%gif%' and not $field like '%jpg%' and not $field = '' LIMIT $limit OFFSET $offset";
//$sql = "SELECT DISTINCT $field FROM $table WHERE not $field = '' LIMIT $limit OFFSET $offset";
$sql_pages = "$sql LIMIT $limit OFFSET $offset";//jamesqi 2018-5-6
$result_pages = mysqli_query($link,$sql_pages);
$output .= map_start();
while ($row = $result_pages->fetch_array()) {
$value = $row[0];
if ($value != '') {
if (isset($row[1])) {
$value1 = $row[1];
}
$value_urlencode = urlencode($value);
if (strpos($value,'&') == FALSE) {
$output .= "";
if (isset($row[1]) && !is_date_time($value1)) {
$output .= "https://$http_host/$path/$value_urlencode/".urlencode($row[1])."";
} else {
$output .= "https://$http_host/$path/$value_urlencode";
}
if (isset($row[1]) && is_date_time($value1)) {
$output .= "".str_replace(' ','T',$value1)."Z";
} else {
$output .= "$lastmod";
}
$output .= "$changefreq";
$output .= "$priority";
$output .= "\n";
}
}
}
$output .= map_end();
header('content-type: application/x-gzip');
$output = gzencode($output, 9);//2021-6-16
print $output;
}
......
上面代碼中紅色部分是這次修改或者新增的。
這樣對于索引型頁面和隻有一頁長度的網站地圖頁面是不采用gz壓縮的,隻對索引頁指向的多個頁面采用gz壓縮。
一個索引頁的例子,裡面指向的多個網站地圖頁都是.xml.gz的。
2022年10月27日補充:同事數次反應百度資源平台中網站地圖的獲取網址有問題,再反複查看多個網站的情況,發現隻要是xml.gz的文件百度都無法獲取其中的網址,在網上搜索“百度網站地圖sitemap壓縮”等字樣,找到一些老文章,例如《百度主動推送,百度收錄,百度sitemap.xml的标準格式...》,裡面說支持gzip壓縮,但又查看官方最新的文章《百度資源平台工具使用手冊 - 普通收錄》,裡面卻沒有支持壓縮的内容,估計是百度以前支持壓縮格式,後來又取消了支持。資源平台中顯示:
https://example.com/sitemap_123.xml.gz?page=123 存在無效url 0 2022-10-26 07:08:36
詳細情況:

隻有把主要是百度來源流量的網站進行了修改,網站地圖不再使用gz壓縮。同樣,國内的搜狗、頭條等搜索引擎都不支持sitemap的壓縮格式,而國外的Google、Bing等搜索引擎都支持sitemap的壓縮。
评论