
近期集中為以前的一些網站添加了手機版,為了讓搜索引擎優化的效果更佳,我們為手機版都添加了Mobile XML Sitemap,在MediaWiki網站中是逐一對電腦版中的地圖文件進行人工修改保存,而在Drupal網站中我們采用的辦法是自動實時轉換,下面記錄幾個要點:
- 在設置手機版網站的時候sites/m.example.com/files目錄可以做一個軟鍊接到對應的電腦版sites/www.example.com/files目錄,這樣手機版就可以找到對應的站點地圖文件;
- 這個找到的站點地圖内容卻是電腦版的,需要進行轉換,我的辦法是修改sites/all/modules/xmlsitemap/xmlsitemap.page.inc來實現,具體代碼在後面;
- 修改後的xml sitemap有可能超過10M的大小限制,可以采取預先設置電腦版站點地圖每個最多包含2.5萬的辦法來解決;
- 修改成功後,可以修改robots.txt來告知搜索引擎新的站點地圖URL,并到Google Webmaster Tools、百度站長平台中主動添加網站、提交地圖。
xmlsitemap.page.inc中修改的代碼:
/*
while (!feof($handle)) {
print fread($handle, 1024*16);
}
*/
//jamesqi 2012-5
$content="";
while (!feof($handle)) {
$content.=fread($handle, 1024*16);
}
fclose($handle);
$server=$_SERVER['SERVER_NAME'];
switch ($server) {
case 'm.114.mingluji.com':
$source='114.mingluji.com';
break;
case 'm.sale.mingluji.com':
$source='sale.mingluji.com';
break;
case 'm.goumai.mingluji.com':
$source='goumai.mingluji.com';
break;
default:
$source=$server;
}
if ($source!=$server) {
$content=str_replace("http://$source","http://$server",$content);
$content=str_replace('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">','<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">',$content);
$content=str_replace('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">','<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">',$content);
$content=str_replace('</url>','<mobile:mobile/></url>',$content);
}
print $content;
從上面可以看出,讀取硬盤上的xml文件後,先進行了幾個替換,然後再輸出就符合Mobile XML Sitemap的規範了。
评论2
子域名和子目錄兩種手機版都支持
改了一下程序,子域名和子目錄兩種手機版都支持sitemap變換:
function xmlsitemap_file_transfer($uri, $headers) { if (ob_get_level()) { ob_end_clean(); } foreach ($headers as $name => $value) { drupal_add_http_header($name, $value); } drupal_send_headers(); // Attempt to increase time to transfer file. drupal_set_time_limit(240); $scheme = variable_get('file_default_scheme', 'public'); // Transfer file in 16 KB chunks to save memory usage. if ($scheme && file_stream_wrapper_valid_scheme($scheme) && $fd = fopen($uri, 'rb')) { //delete start /* jamesqi 2012-12-23 while (!feof($fd)) { print fread($fd, 1024*16); } fclose($fd); */ //delete end //insert start $content=""; while (!feof($fd)) { $content.=fread($fd, 1024*16); } fclose($fd); $server=$_SERVER['SERVER_NAME']; $path=$_SERVER['REQUEST_URI']; if (substr($server,0,2)=='m.') { $source=substr($server,2); $target=$server; } elseif (substr($path,0,3)=='/m/') { $source=$server; $target="$server/m"; } else { $source=$server; $target=$server; } if ($source!=$target) { $content=str_replace("http://$source","http://$target",$content); $content=str_replace('<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">','<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">',$content); $content=str_replace('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">','<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">',$content); $content=str_replace('</url>','<mobile:mobile/></url>',$content); } print $content; //insert end } else { drupal_not_found(); } drupal_exit(); }多語言的網站地圖也可以采用轉換方式産生
請看新博文《Drupal網站多語言版的站點地圖自動轉換》,其中對手機版的代碼也進行了修改完善。