最近在研究一套經過二次開發的原始碼準備要上線,測試時發現這套框架當初設計就沒有針對SEO做最佳化,Sitemap是需要自己額外寫程式碼去產出。
這對早已習慣用WordPress的SEO造成困擾,因為早已忘記Sitemap和Robots要如何設定。
Table of Contents
Robots設定
提供Robots是提供規則,告訴爬蟲你可以抓取或者禁止抓取哪些資料,但屬於道德規範、若爬蟲不遵守規則你也不能怎樣。
所以當有特定目錄或連結不開放爬蟲,可以在nginx或apache針對資料夾設訪問權限阻擋惡意爬蟲。
首先創建一個檔案,檔名命名為robot.txt
接著依照下面的設定介紹將程式碼放到檔案內,最後放到網站根目錄下即可。
常見設定
允許所有爬蟲
User-agent: *
Allow: /
只允許Google爬蟲,其他爬蟲禁止
User-agent: Googlebot
Allow: /
禁止所有爬蟲
User-agent: *
Disallow: /
禁止所有爬蟲訪問指定目錄
User-agent: *
Disallow: /cgi-bin/
Disallow: /images/
完整範本
允許所有爬蟲,但是cgi-bin和images禁止抓取資料
User-agent: *
Allow: /
Disallow: /cgi-bin/
Disallow: /images/
Sitemap設定
基本上有使用Search Console網站管理工具,網站資源新增後Google也是會安排爬蟲抓取、但是速度會比較慢。
這時就需要手動寫一份Sitemap提交給Google加速收錄。
首先要創建一個sitemap.xml檔案,接著依照下面的設定介紹將程式碼放到檔案內,最後一樣放到網站根目錄下即可。
填寫說明
創建好檔案後,下面這一串資料不用變動直接複製貼上
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
這邊一樣直接複製貼上,但是<loc>內的網址要改成自己的
<lastmod>請填當天日期,如果後續有要提交新的、記得更改日期
<url>
<loc>https://rdfarm.net/</loc>
<lastmod>2025-02-09</lastmod>
</url>
</urlset>
完整範本
這邊會看到sitemap裡提交兩個網址,如果你有多個網址要提交就直接複製修改。
但是你的網站頁面有數百個,請直接寫程式生成或者參考下面的工具。
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->
<url>
<loc>https://rdfarm.net/</loc>
<lastmod>2025-02-09</lastmod>
</url>
<url>
<loc>https://rdfarm.net/linux-ssh-key/</loc>
<lastmod>2025-02-09</lastmod>
</url>
</urlset>
Sitemap產生工具
如果你不會程式也不會用AI寫程式,那這套工具可以快速製作一份網站sitemap。
不過免費版只提供500個頁面,升級Pro版支付4.19美金可以提升到1000個頁面。
工具資訊
名稱: XML-Sitemaps.com
網址: https://www.xml-sitemaps.com/
首先前往該網址,輸入要製作sitemap的網站,然後點擊START

這邊會開始抓取網站資料,需要一點時間等待

完成之後點擊「VIEW SITEMAP DETALS」就可以查看結果

在「Sitemap Preview」中可以查看抓取的結果是否有誤,如果沒問題就可以點擊「DOWNLOAD YOUR XML SITEMAP FILE」下載檔案

將檔案開啟後,就會看到完整XML格式的sitemap
