兩大屬性體系:`name` vs `http-equiv`
| 屬性 | 用途 | 對應值屬性 |
| `name` | 描述網頁資訊(給搜尋引擎、瀏覽器、社群分享) | `content` |
| `http-equiv` | 模擬 HTTP 回應標頭(給瀏覽器解析指令) | `content` |
`name` 屬性:SEO 與社群分享核心
| name 值 | 用途 | content 範例 | 備註 |
| `description` | 頁面描述(搜尋結果摘要) | `"完整教學 HTML meta 標籤..."` | **最重要**,150-160 字,唯一、描述性、含關鍵字 |
| `keywords` | 關鍵字(已被 Google 忽略) | `"HTML, meta, SEO, 教學"` | 可保留給內部搜尋/其他引擎,不害處 |
| `robots` | 控制爬蟲行為 | `index, follow` / `noindex, nofollow` / `noarchive` / `nosnippet` | 多值逗號分隔,大小寫不敏感 |
| `author` | 作者資訊 | `"John Doe, john@example.com"` | 可配合 `` |
| `viewport` | 行動視窗設定 | `width=device-width, initial-scale=1.0` | **RWD 必備** |
| `theme-color` | 瀏覽器標籤顏色 | `#1a73e8` | Chrome/Edge/Safari 行動版 |
| `generator` | 產生工具 | `"Astro v4.0"` | 資訊性 |
| `application-name` | Web App 名稱 | `"My PWA"` | PWA 安裝提示用 |
| `referrer` | Referrer Policy | `no-referrer`, `origin`, `strict-origin-when-cross-origin` | 或用 HTTP 標頭 `Referrer-Policy` |
Open Graph (og:) 與 Twitter Card 社群分享標籤
<!-- Open Graph (Facebook, LinkedIn, LINE, Discord...) -->
<meta property="og:type" content="article"> /* website, article, video... */
<meta property="og:title" content="HTML meta 完整指南">
<meta property="og:description" content="深入解析 meta 標籤...">
<meta property="og:url" content="https://example.com/html-meta-guide">
<meta property="og:image" content="https://example.com/og-image.png"> /* 1200x630px 建議 */
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta property="og:locale" content="zh_TW">
<meta property="og:site_name" content="技術部落格">
<meta property="article:published_time" content="2024-01-15T10:00:00+08:00">
<meta property="article:author" content="https://example.com/author/john">
<!-- Twitter Card -->
<meta name="twitter:card" content="summary_large_image"> /* summary, summary_large_image, app, player */
<meta name="twitter:site" content="@techblog">
<meta name="twitter:creator" content="@john">
<meta name="twitter:title" content="HTML meta 完整指南">
<meta name="twitter:description" content="深入解析...">
<meta name="twitter:image" content="https://example.com/twitter-image.png"> /* 可同 og:image */
`http-equiv` 屬性:模擬 HTTP 標頭
| http-equiv 值 | 對應 HTTP 標頭 | 用途與範例 | 備註 |
| `Content-Type` | `Content-Type` | `text/html; charset=UTF-8` | **建議改用 ``**,更簡潔優先級高 |
| `Content-Security-Policy` | `Content-Security-Policy` | `default-src 'self'; script-src 'self' https://cdn.example.com;` | **CSP 防 XSS 核心**,建議用 HTTP 標頭,meta 受限(不支援 `report-uri`、worker 等) |
| `X-UA-Compatible` | `X-UA-Compatible` | `IE=edge` | 強制 IE 使用最新引擎,**現代專案可移除** |
| `refresh` | `Refresh` | `5; url=https://example.com/new-page` | **慎用**:5 秒後導向,干擾使用者、SEO 負面、無障礙問題。改用 HTTP 301/302 或 JS 延遲導向 |
| `expires` | `Expires` | `Wed, 31 Dec 2025 23:59:59 GMT` | 快取過期時間,**建議用 HTTP 標頭 `Cache-Control`** |
| `pragma` | `Pragma` | `no-cache` | HTTP/1.0 相容,**建議用 `Cache-Control: no-cache`** |
| `set-cookie` | `Set-Cookie` | `session=abc; Path=/; Secure; HttpOnly; SameSite=Lax` | **極不推薦**用 meta 設 Cookie,安全性差、難控制,用 HTTP 標頭 |
| `Window-target` | 無對應標頭 | `_top` | 防止被 iframe 嵌入,**建議用 HTTP `X-Frame-Options: DENY` 或 CSP `frame-ancestors 'none'`** |
安全相關標籤(現代最佳實踐)
<!-- CSP:建議用 HTTP 標頭,meta 為備援 -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'nonce-{RANDOM}'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' https:; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self';">
<!-- 強制 HTTPS(HSTS 預載清單更好) -->
<!-- meta 無法設定 HSTS,需 HTTP 標頭 Strict-Transport-Security -->
<!-- 防止 MIME 類型嗅探 -->
<meta http-equiv="X-Content-Type-Options" content="nosniff">
<!-- 防止點擊劫持 -->
<meta http-equiv="X-Frame-Options" content="DENY">
<!-- 或 CSP frame-ancestors -->
<!-- 啟用 XSS 過濾器(舊 IE/Edge,現代瀏覽器已內建更強機制) -->
<meta http-equiv="X-XSS-Protection" content="1; mode=block">
常見錯誤與修正
| 錯誤 | 影響 | 修正 |
| 重複 `description`/`title` | SEO 稀釋、搜尋結果顯示錯誤 | 每頁唯一 |
| `keywords` 堆砌關鍵字 | 可能被視為垃圾、Google 完全忽略 | 移除或保留 3-5 核心詞 |
| `robots: noindex` 但頁面在 sitemap | 搜尋引擎困惑、浪費抓取預算 | 移出 sitemap 或改 `index` |
| `refresh` 導向 | 使用者困惑、無障礙失敗、SEO 損失 | 用 301/302 重導向 |
| 用 meta 設 CSP/Cookie/X-Frame-Options | 安全性受限、部分指令無效、難維護 | **全移至 HTTP 回應標頭**(Nginx/Apache/CDN/應用層中介軟體) |
| 缺少 `viewport` | 行動版縮小顯示、需雙指縮放 | 加上 `width=device-width, initial-scale=1.0` |
驗證工具
**核心原則**:`` 專注 SEO/社群描述,`` 能用 HTTP 標頭就用 HTTP 標頭(CSP、快取、安全、導向),只保留 `charset`、`viewport`、`theme-color` 等無法用標頭設定的項目。