網站地圖的作用,是讓搜索引擎更快、更完整地收錄站內頁面。
整個互聯網像一張縱橫交錯的網:節點是網頁,网页之間透過 URL 相互連接。爬蟲從一個頁面出發,沿著頁面上的連結爬到下一頁,再從下一頁繼續往外擴散。問題在於,一個新發布的站點如果沒有任何外部連結指向它,就永遠不會被爬到。
解法之一是主動向搜索引擎提交網址,但提交時通常只能給一個首頁。為了讓所有 URL——尤其是動態生成的那些——都能被爬蟲便利地檢索到,你需要一份完整、結構清晰、更新及時的網站地圖。
實作上,用 .ashx 泛型處理常式動態輸出符合 sitemaps.org 規範的 XML,是最省事的做法。地圖產生之後提交給搜索引擎即可。大量實踐證明,提交網站地圖能顯著提升收錄速度與深度。其他優化手法效果難以驗證、可能失效甚至帶來副作用,提交網站地圖是少數例外。
Linq to XML 在這件事上的操作體驗相當順手。
<%@ WebHandler Language="C#" Class="website" %>
using System;
using System.Web;
using System.Xml.Linq;
public class website : IHttpHandler {
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/xml";
// 宣告資訊;standalone 設為 yes 表示此 XML 自含,不依賴外部 DTD
XDeclaration declaration = new XDeclaration("1.0", "UTF-8", "yes");
context.Response.Write(declaration);
XNamespace ns = "https://www.sitemaps.org/schemas/sitemap/0.9";
XElement siteMap = new XElement(ns + "urlset");
string fixedUrl = "https://www.example.com/article";
string wholeUrl = string.Empty;
// 遍歷資料,轉為 XML 節點
foreach (var item in Articles.GetArticles()) {
XElement url = new XElement("url");
wholeUrl = string.Format("{0}?id={1}&catelog={2}", fixedUrl, item.ID, item.Catelog);
XElement loc = new XElement("loc", wholeUrl);
XElement lastmod = new XElement("lastmod", item.LastMod.ToShortDateString());
XElement changefreq = new XElement("changefreq", item.Frequency);
XElement priority = new XElement("priority", item.Weight);
url.Add(loc, lastmod, changefreq, priority);
siteMap.Add(url);
}
context.Response.Write(siteMap);
}
public bool IsReusable { get { return false; } }
}
<%@ WebHandler Language="C#" Class="rss" %>
using System;
using System.Web;
using System.Xml.Linq;
public class rss : IHttpHandler {
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/xml";
context.Response.Write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
XElement rssFeed = new XElement("rss", new XAttribute("version", "2.0"));
string fixedUrl = "https://www.example.com/article";
string wholeUrl = string.Empty;
XElement channel = new XElement("channel",
new XElement("title", "example"),
new XElement("link", fixedUrl),
new XElement("description", "site description"),
new XElement("pubDate", DateTime.Now.ToString())
);
foreach (var article in Articles.GetArticles()) {
XElement item = new XElement("item");
XElement title = new XElement("title", article.Title);
wholeUrl = string.Format("{0}?id={1}&catelog={2}", fixedUrl, article.ID, article.Catelog);
XElement link = new XElement("link", wholeUrl);
XElement description = new XElement("description", article.Description);
XElement pubDate = new XElement("pubDate", article.LastMod.ToString());
item.Add(title, link, description, pubDate);
channel.Add(item);
}
rssFeed.Add(channel);
context.Response.Write(rssFeed);
}
public bool IsReusable { get { return false; } }
}
public class Article
{
public int ID;
public string Catelog;
public DateTime LastMod;
public double Weight;
public Freq Frequency;
public string Title;
public string Description;
}
public enum Freq { none = 1, daily = 2, weekly = 3 }
實際部署時有兩個細節值得注意:sitemap 的命名空間要用 sitemaps.org 的正式位址,寫錯會導致提交失敗;lastmod 應該輸出符合 W3C 日期格式的字串, ToShortDateString() 會受伺服器地區設定影響,較穩妥的做法是明確指定格式。