Sitemap Index
Sitemap Index(sitemap 索引)是管理多個 sitemap 檔案的 XML 目錄。學習何時需要使用 sitemap index、如何正確設定、以及大型網站的 sitemap 分割最佳實踐。
定義
Sitemap Index(sitemap 索引檔)是一個 XML 檔案,用來列出並管理多個 sitemap 檔案的位置。根據 sitemaps.org 協議,單一 sitemap 最多只能包含 50,000 個 URL 且檔案大小不得超過 50MB(未壓縮)。當網站 URL 數量超過這個限制,或者你希望按內容類型(文章、產品、分類)、語言版本、更新頻率來分別管理 sitemap 時,就需要使用 sitemap index。Google 和其他搜尋引擎會自動讀取 sitemap index 並抓取其中列出的所有子 sitemap。
為什麼重要
- 突破單一 sitemap 50,000 URL / 50MB 的限制,大型網站必備
- 按內容類型分類(posts、pages、products、categories)更易於管理與除錯
- 按語言/地區分割(sitemap-en.xml、sitemap-zh.xml)支援國際化 SEO
- 提高更新效率:只需重新產生有變動的子 sitemap,減少伺服器負載
- 在 Google Search Console 只需提交一個 index,自動涵蓋所有子 sitemap
- 更清楚追蹤索引狀態:可以在 GSC 中分別查看每個子 sitemap 的索引情況
- 支援動態產生:可以根據資料庫動態產生多個 sitemap
怎麼做(實作重點)
- 評估是否需要:URL < 10,000 且結構簡單的小站通常不需要,單一 sitemap 即可
- 決定分割策略:按內容類型(最常見)、按更新頻率、按語言、或按 URL 數量均分
- 產生子 sitemap:每個檔案包含同類型的 URL,確保不超過 50,000 條
- 建立 sitemap index:使用 <sitemapindex> 標籤列出所有子 sitemap 的 <loc> 和 <lastmod>
- 設定自動更新機制:內容變更時自動更新對應的子 sitemap 和 index 的 lastmod
- 在 robots.txt 加入 Sitemap 指令:Sitemap: https://example.com/sitemap-index.xml
- 提交到 Google Search Console 並監控索引狀態
範例
xml
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<!-- 靜態頁面 -->
<sitemap>
<loc>https://example.com/sitemap-pages.xml</loc>
<lastmod>2025-01-15</lastmod>
</sitemap>
<!-- 部落格文章 -->
<sitemap>
<loc>https://example.com/sitemap-posts.xml</loc>
<lastmod>2025-01-20</lastmod>
</sitemap>
<!-- 產品頁面 -->
<sitemap>
<loc>https://example.com/sitemap-products.xml</loc>
<lastmod>2025-01-18</lastmod>
</sitemap>
<!-- 多語言:英文版 -->
<sitemap>
<loc>https://example.com/sitemap-en.xml</loc>
<lastmod>2025-01-20</lastmod>
</sitemap>
<!-- 多語言:中文版 -->
<sitemap>
<loc>https://example.com/sitemap-zh.xml</loc>
<lastmod>2025-01-19</lastmod>
</sitemap>
</sitemapindex>javascript
// Node.js 動態產生 sitemap index 範例
const generateSitemapIndex = (sitemaps) => {
const header = '<?xml version="1.0" encoding="UTF-8"?>';
const indexStart = '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
const indexEnd = '</sitemapindex>';
const entries = sitemaps.map(({ url, lastmod }) => `
<sitemap>
<loc>${url}</loc>
<lastmod>${lastmod}</lastmod>
</sitemap>`).join('');
return `${header}\n${indexStart}${entries}\n${indexEnd}`;
};
// 使用範例
const sitemaps = [
{ url: 'https://example.com/sitemap-posts.xml', lastmod: '2025-01-20' },
{ url: 'https://example.com/sitemap-products.xml', lastmod: '2025-01-18' },
];
console.log(generateSitemapIndex(sitemaps));相關連結
常見問題
關於這個詞彙的常見問答。