如何禁止搜尋引擎索引網站(Noindex 實務指南)

當內部系統放到網路上,不希望被不相關的人看到,首先就要讓正規爬蟲知道不要蒐集哪些資訊

1. 為何要禁止搜尋引擎索引?

  • 測試或開發中的網站不應被公開搜尋
  • 公司內部系統或限定訪問頁面避免洩漏
  • 避免重複內容影響 SEO 排名

2. 禁止搜尋引擎的常見方法總覽

方法 說明 是否可被搜尋引擎遵守
meta robots tag HTML 標籤插入於 <head> ✅ 支援 Google/Bing 等
robots.txt 告知爬蟲哪些路徑不要抓取 ⚠️ 建議搭配 noindex 使用
X-Robots-Tag 用 HTTP header 傳送指示 ✅ 適用所有檔案類型(支援圖片、PDF 等非 HTML 檔案)
密碼保護 用 HTTP 認證完全阻擋訪問 ✅ 爬蟲無法存取

3. 各種實作方式與範例

3.1 使用 <meta name="robots" content="noindex">

在所有 HTML 頁面的 <head> 中加入:

<meta name="robots" content="noindex, nofollow">

3.2 設定 robots.txt

放在網站根目錄下的 robots.txt

禁止全部爬蟲爬整個網站

User-agent: *
Disallow: /

⚠️ 注意:robots.txt 只能阻擋抓取,但不能阻止索引(如果 URL 已被發現,仍可能出現在搜尋結果中)

3.3 設定 X-Robots-Tag HTTP 標頭(伺服器方式)

🔧 Apache

.htaccess 或虛擬主機設定中加入:

<IfModule mod_headers.c>
  Header always set X-Robots-Tag "noindex, nofollow"
</IfModule>

🔧 Nginx(一般)

serverlocation 區塊中加入:

add_header X-Robots-Tag "noindex, nofollow" always;

🔧 Nginx Reverse Proxy

server {
  listen 80;
  server_name example.com;

  location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    add_header X-Robots-Tag "noindex, nofollow" always;
  }
}

4. 如何驗證是否生效?

  • curl -I https://yourdomain.com,應看到 X-Robots-Tag: noindex, nofollow
  • 瀏覽器開發者工具 → Network → 檢查主文件 Response Headers

5. 注意事項與常見誤區

  • robots.txt 無法完全防止索引,需搭配 noindex
  • 若用 meta tag 封鎖,但又在 robots.txt 阻擋該頁,爬蟲可能無法看到 meta,導致索引仍存在
  • 若想永久移除搜尋結果,應使用搜尋引擎的 URL 移除工具(如 GSC: Google Search Console Tools)

補充: robots.txt 是搜尋引擎「是否抓取」該頁的開關 如果你在 robots.txt 裡寫 Disallow: /secret.html,Google、Bing 就不會去請求 /secret.html 這個網址 那麼,該頁裡的 <meta name=“robots” content=“noindex”> 就永遠不會被讀取到

這就會導致 該頁的 URL 出現在搜尋結果中 但內容是空的、只有網址和可能的 anchor 文字(出現在其他頁的連結文字)

6. 參考資源與延伸閱讀