peakchao

搜索

peakchao

peakchao

前端开发工程师 | Go 爱好者

联系方式

SEO 完全指南:从入门到精通

peakchao 2025-12-13 05:46 12 次浏览 0 条评论

什么是 SEO

SEO 的定义

SEO(Search Engine Optimization,搜索引擎优化) 是一系列用于提升网站在搜索引擎自然搜索结果中排名的技术和策略。

┌─────────────────────────────────────────────────────────────────┐
│                     搜索结果页面 (SERP)                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   🔍 搜索框: "如何学习编程"                                      │
│   ─────────────────────────────────────────────────────────────│
│                                                                 │
│   📢 广告 (PPC/SEM)                                             │
│   ├── 某编程培训机构 - 0元试听                                   │
│   └── 在线编程课程 - 限时优惠                                    │
│                                                                 │
│   ─────────────────────────────────────────────────────────────│
│                                                                 │
│   🌐 自然搜索结果 (SEO 优化目标)                                 │
│   │                                                             │
│   ├── 1. 编程入门指南 - 从零开始学编程 | example.com            │
│   │      完整的编程学习路线,包含 Python、JavaScript...         │
│   │                                                             │
│   ├── 2. 2024年最佳编程学习资源 | coding.com                    │
│   │      精选的免费编程教程和在线课程推荐...                     │
│   │                                                             │
│   └── 3. 自学编程的正确方法 | blog.dev                          │
│          经验分享:如何高效学习编程...                           │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

为什么 SEO 重要

优势说明
免费流量不同于付费广告,自然搜索流量无需持续投入
高质量用户搜索用户有明确意图,转化率更高
长期效果良好的排名可以持续带来流量
品牌信任排名靠前的网站更容易获得用户信任
竞争优势超越竞争对手获取更多市场份额

SEO 的三大支柱

┌─────────────────────────────────────────────────────────────────┐
│                        SEO 三大支柱                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│         ┌─────────────┐                                         │
│         │   技术 SEO   │                                         │
│         │  Technical  │                                         │
│         └──────┬──────┘                                         │
│                │                                                │
│    ┌───────────┴───────────┐                                    │
│    │                       │                                    │
│    ▼                       ▼                                    │
│ ┌─────────────┐     ┌─────────────┐                             │
│ │  页面 SEO   │     │  站外 SEO   │                             │
│ │  On-Page   │     │  Off-Page  │                             │
│ └─────────────┘     └─────────────┘                             │
│                                                                 │
│  • 网站架构          • 内容质量         • 外部链接              │
│  • 爬虫可访问性       • 关键词优化       • 社交信号              │
│  • 网站速度          • 用户体验         • 品牌提及              │
│  • 移动适配          • 内部链接         • 本地 SEO              │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

搜索引擎工作原理

爬虫抓取(Crawling)

搜索引擎使用称为“爬虫“或“蜘蛛“的程序自动访问和发现网页。

┌─────────────────────────────────────────────────────────────────┐
│                       爬虫工作流程                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌──────────┐                                                  │
│   │ 种子 URL │ ──► 已知网站列表                                  │
│   └────┬─────┘                                                  │
│        │                                                        │
│        ▼                                                        │
│   ┌──────────┐     ┌──────────┐     ┌──────────┐               │
│   │ 获取页面 │ ──► │ 解析内容 │ ──► │ 提取链接 │               │
│   └────┬─────┘     └──────────┘     └────┬─────┘               │
│        │                                  │                     │
│        │         ┌────────────────────────┘                     │
│        │         │                                              │
│        ▼         ▼                                              │
│   ┌──────────────────┐                                          │
│   │   待爬取队列      │ ──► 循环继续爬取                         │
│   └──────────────────┘                                          │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

影响爬虫抓取的因素:

  • robots.txt:告诉搜索引擎哪些页面可以/不可以抓取
  • sitemap.xml:提供网站结构地图,帮助发现页面
  • 链接结构:内部链接帮助爬虫发现更多页面
  • 抓取预算:大型网站需要优化让爬虫高效抓取重要页面

索引(Indexing)

爬虫抓取页面后,搜索引擎会分析内容并存入索引数据库。

┌─────────────────────────────────────────────────────────────────┐
│                        索引过程                                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   原始 HTML                      索引数据库                      │
│   ┌─────────────────┐           ┌─────────────────────────────┐ │
│   │ <html>          │           │ 关键词 → 页面映射            │ │
│   │   <title>       │   ──►     │                             │ │
│   │     编程教程    │   处理     │ "编程" → [page1, page5...]  │ │
│   │   </title>      │           │ "教程" → [page1, page3...]  │ │
│   │   <body>        │           │ "Python" → [page2, page1...]│ │
│   │     ...         │           │                             │ │
│   │   </body>       │           │ 页面属性:                   │ │
│   │ </html>         │           │ • 标题、描述                │ │
│   └─────────────────┘           │ • 发布时间                  │ │
│                                 │ • 页面质量分数              │ │
│                                 │ • 移动友好性                │ │
│                                 └─────────────────────────────┘ │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

排名(Ranking)

当用户搜索时,搜索引擎从索引中检索相关页面并按相关性排序。

Google 核心排名因素:

因素类别具体因素权重
内容相关性关键词匹配、语义相关、内容深度★★★★★
用户体验Core Web Vitals、移动友好、安全性★★★★☆
权威性外部链接、品牌信号、E-E-A-T★★★★☆
技术因素页面速度、结构化数据、HTTPS★★★☆☆

E-E-A-T: Experience(经验)、Expertise(专业性)、Authoritativeness(权威性)、Trustworthiness(可信度)


技术 SEO

技术 SEO 是确保搜索引擎能够有效抓取和索引网站的基础工作。

robots.txt 配置

robots.txt 文件位于网站根目录,用于指导搜索引擎爬虫的抓取行为。

# robots.txt 示例

# 所有爬虫的规则
User-agent: *
Allow: /
Disallow: /admin/
Disallow: /api/
Disallow: /private/
Disallow: /*?*sort=
Disallow: /*?*filter=

# 禁止特定爬虫
User-agent: BadBot
Disallow: /

# 指定 sitemap 位置
Sitemap: https://example.com/sitemap.xml

常用指令说明:

指令说明示例
User-agent指定爬虫User-agent: Googlebot
Allow允许抓取Allow: /public/
Disallow禁止抓取Disallow: /admin/
Sitemap站点地图位置Sitemap: https://example.com/sitemap.xml
Crawl-delay爬取延迟(秒)Crawl-delay: 10

⚠️ 注意robots.txt 只是一个建议,恶意爬虫可能会忽略它。敏感内容应使用身份验证保护。

Sitemap.xml 配置

站点地图帮助搜索引擎发现网站的所有重要页面。

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  
  <!-- 首页 -->
  <url>
    <loc>https://example.com/</loc>
    <lastmod>2024-12-07</lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
  </url>
  
  <!-- 文章页 -->
  <url>
    <loc>https://example.com/blog/seo-guide</loc>
    <lastmod>2024-12-01</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.8</priority>
  </url>
  
  <!-- 分类页 -->
  <url>
    <loc>https://example.com/category/tech</loc>
    <lastmod>2024-11-30</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.6</priority>
  </url>
  
</urlset>

Go 动态生成 Sitemap:

package handlers

import (
    "encoding/xml"
    "net/http"
    "time"
)

type URLSet struct {
    XMLName xml.Name `xml:"urlset"`
    Xmlns   string   `xml:"xmlns,attr"`
    URLs    []URL    `xml:"url"`
}

type URL struct {
    Loc        string `xml:"loc"`
    LastMod    string `xml:"lastmod,omitempty"`
    ChangeFreq string `xml:"changefreq,omitempty"`
    Priority   string `xml:"priority,omitempty"`
}

func (h *Handler) Sitemap(w http.ResponseWriter, r *http.Request) {
    baseURL := "https://example.com"
    
    urls := []URL{
        {
            Loc:        baseURL + "/",
            LastMod:    time.Now().Format("2006-01-02"),
            ChangeFreq: "daily",
            Priority:   "1.0",
        },
    }
    
    // 获取所有已发布文章
    posts, _ := h.postRepo.GetPublished()
    for _, post := range posts {
        urls = append(urls, URL{
            Loc:        baseURL + "/post/" + post.Slug,
            LastMod:    post.UpdatedAt.Format("2006-01-02"),
            ChangeFreq: "weekly",
            Priority:   "0.8",
        })
    }
    
    sitemap := URLSet{
        Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
        URLs:  urls,
    }
    
    w.Header().Set("Content-Type", "application/xml")
    xml.NewEncoder(w).Encode(sitemap)
}

URL 结构优化

良好的 URL 结构对 SEO 和用户体验都很重要。

URL 最佳实践:

✅ 好的 URL 示例:
https://example.com/blog/seo-complete-guide
https://example.com/products/iphone-15-pro
https://example.com/category/web-development

❌ 差的 URL 示例:
https://example.com/p?id=12345
https://example.com/blog/post/2024/12/07/this-is-a-very-long-title-about-seo
https://example.com/BLOG/SEO-Guide (大小写混用)
规则说明
使用小写URL 区分大小写,统一小写避免重复
用连字符分隔使用 - 而非 _ 或空格
简洁明了包含关键词,但不要过长
避免参数静态 URL 优于动态参数
层级清晰反映网站结构

Canonical 标签

用于解决重复内容问题,告诉搜索引擎哪个是“权威“版本。

<!-- 在重复页面中声明规范 URL -->
<link rel="canonical" href="https://example.com/blog/seo-guide" />

常见使用场景:

场景1:带参数的URL
https://example.com/products?color=red
https://example.com/products?color=blue
                    ↓
        canonical → https://example.com/products

场景2:HTTP/HTTPS 版本
http://example.com/page
https://example.com/page
                    ↓
        canonical → https://example.com/page

场景3:分页内容
https://example.com/blog?page=1
https://example.com/blog?page=2
                    ↓
        canonical → https://example.com/blog

HTTPS 安全

Google 明确将 HTTPS 作为排名因素之一。

┌─────────────────────────────────────────────────────────────────┐
│                     HTTPS 对 SEO 的影响                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   🔒 HTTPS 网站                    🔓 HTTP 网站                  │
│   ┌─────────────────────┐         ┌─────────────────────┐       │
│   │ • 排名加分          │         │ • 浏览器警告        │       │
│   │ • 用户信任度高       │         │ • 数据可被窃听      │       │
│   │ • 可用 HTTP/2       │         │ • 无法用新特性      │       │
│   │ • 保护用户数据       │         │ • 转化率降低        │       │
│   └─────────────────────┘         └─────────────────────┘       │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

页面渲染方式

搜索引擎对不同渲染方式的处理能力不同。

渲染方式SEO 友好度说明
SSR(服务端渲染)★★★★★爬虫直接获取完整 HTML
SSG(静态生成)★★★★★预渲染静态页面,速度最快
CSR(客户端渲染)★★☆☆☆需要等待 JS 执行,爬虫可能无法正确解析
ISR(增量静态再生)★★★★☆兼顾静态性能和内容更新

React/Next.js 服务端渲染示例:

// pages/post/[slug].tsx (Next.js)
import { GetServerSideProps } from 'next';
import Head from 'next/head';

interface PostProps {
  post: {
    title: string;
    description: string;
    content: string;
  };
}

export default function Post({ post }: PostProps) {
  return (
    <>
      <Head>
        <title>{post.title} | My Blog</title>
        <meta name="description" content={post.description} />
      </Head>
      <article>
        <h1>{post.title}</h1>
        <div dangerouslySetInnerHTML={{ __html: post.content }} />
      </article>
    </>
  );
}

export const getServerSideProps: GetServerSideProps = async ({ params }) => {
  const post = await fetchPost(params?.slug as string);
  
  if (!post) {
    return { notFound: true };
  }
  
  return { props: { post } };
};

页面 SEO

页面 SEO 关注的是单个页面的优化,确保内容对搜索引擎和用户都有价值。

Meta 标签优化

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <!-- 基础 Meta 标签 -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <!-- SEO 核心标签 -->
    <title>SEO 完全指南:从入门到精通 | 技术博客</title>
    <meta name="description" content="全面介绍 SEO 优化技术,包括技术 SEO、页面优化、结构化数据等,帮助开发者提升网站搜索排名。">
    <meta name="keywords" content="SEO,搜索引擎优化,网站优化,技术SEO">
    <meta name="author" content="作者名">
    
    <!-- Canonical URL -->
    <link rel="canonical" href="https://example.com/blog/seo-guide">
    
    <!-- Open Graph (Facebook/微信) -->
    <meta property="og:type" content="article">
    <meta property="og:title" content="SEO 完全指南:从入门到精通">
    <meta property="og:description" content="全面介绍 SEO 优化技术...">
    <meta property="og:image" content="https://example.com/images/seo-guide-cover.jpg">
    <meta property="og:url" content="https://example.com/blog/seo-guide">
    <meta property="og:site_name" content="技术博客">
    
    <!-- Twitter Card -->
    <meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:title" content="SEO 完全指南:从入门到精通">
    <meta name="twitter:description" content="全面介绍 SEO 优化技术...">
    <meta name="twitter:image" content="https://example.com/images/seo-guide-cover.jpg">
    
    <!-- 其他重要标签 -->
    <meta name="robots" content="index, follow">
    <link rel="alternate" hreflang="zh-CN" href="https://example.com/blog/seo-guide">
    <link rel="alternate" hreflang="en" href="https://example.com/en/blog/seo-guide">
</head>

标题优化规则

┌─────────────────────────────────────────────────────────────────┐
│                    Title 标签最佳实践                            │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ✅ 好的标题示例:                                              │
│   ├── "SEO 入门指南:2024 年必备技巧 | 技术博客"                 │
│   ├── "Python 教程:从零开始学编程(含实例)"                    │
│   └── "iPhone 15 Pro 评测:值不值得买?"                        │
│                                                                 │
│   ❌ 差的标题示例:                                              │
│   ├── "首页"(没有关键词)                                      │
│   ├── "SEO SEO优化 SEO技巧 搜索引擎优化"(关键词堆砌)           │
│   └── "这是一篇关于如何学习搜索引擎优化..."(过长)              │
│                                                                 │
│   📏 标题规则:                                                  │
│   • 长度:50-60 个字符                                          │
│   • 关键词靠前                                                  │
│   • 每个页面标题唯一                                            │
│   • 包含品牌名(可选,放最后)                                  │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

标题层级结构

正确使用 H1-H6 标签构建内容层级:

<!-- 正确的标题层级 -->
<h1>SEO 完全指南</h1>                    <!-- 每页只有一个 H1 -->

  <h2>一、什么是 SEO</h2>                <!-- 主要章节 -->
    <h3>1.1 SEO 的定义</h3>              <!-- 子章节 -->
    <h3>1.2 SEO 的重要性</h3>
    
  <h2>二、技术 SEO</h2>
    <h3>2.1 网站架构</h3>
      <h4>2.1.1 URL 结构</h4>            <!-- 更细分的内容 -->
      <h4>2.1.2 内部链接</h4>
    <h3>2.2 网站速度</h3>

常见错误:

<!-- ❌ 错误示例 -->
<h1>公司简介</h1>
<h1>联系我们</h1>          <!-- 多个 H1 -->

<h2>大标题</h2>
<h4>小标题</h4>            <!-- 跳过 H3 -->
<h3>中标题</h3>

图片优化

<!-- 完整的图片 SEO 优化 -->
<figure>
    <img 
        src="/images/seo-diagram.webp"
        alt="SEO 优化流程图:展示从关键词研究到排名提升的完整流程"
        title="SEO 优化流程"
        width="800"
        height="600"
        loading="lazy"
        decoding="async"
    >
    <figcaption>图1:SEO 优化工作流程</figcaption>
</figure>
优化项说明
alt 属性描述图片内容,包含关键词
文件名使用描述性名称如 seo-workflow.jpg
格式优先使用 WebP 格式
尺寸指定 width/height 防止布局偏移
懒加载使用 loading="lazy"
压缩在不影响质量的前提下压缩

内部链接策略

┌─────────────────────────────────────────────────────────────────┐
│                    内部链接结构                                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│                        ┌──────────┐                             │
│                        │   首页   │                             │
│                        └────┬─────┘                             │
│               ┌─────────────┼─────────────┐                     │
│               ▼             ▼             ▼                     │
│         ┌──────────┐ ┌──────────┐ ┌──────────┐                 │
│         │ 分类页A  │ │ 分类页B  │ │ 分类页C  │                 │
│         └────┬─────┘ └────┬─────┘ └────┬─────┘                 │
│              │            │            │                        │
│         ┌────┼────┐  ┌────┼────┐  ┌────┼────┐                  │
│         ▼    ▼    ▼  ▼    ▼    ▼  ▼    ▼    ▼                  │
│        文章 文章 文章...                                        │
│              ↑         ↑                                        │
│              └─────────┘  相关文章互相链接                       │
│                                                                 │
│   最佳实践:                                                    │
│   • 重要页面从首页 3 次点击内可达                               │
│   • 使用描述性锚文本                                            │
│   • 相关内容互相链接                                            │
│   • 避免孤立页面(没有入链的页面)                              │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

结构化数据

结构化数据帮助搜索引擎更好地理解页面内容,并可能获得富媒体搜索结果。

JSON-LD 格式

Google 推荐使用 JSON-LD 格式实现结构化数据:

<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@type": "Article",
    "headline": "SEO 完全指南:从入门到精通",
    "description": "全面介绍 SEO 优化技术,包括技术 SEO、页面优化、结构化数据等。",
    "image": "https://example.com/images/seo-guide-cover.jpg",
    "author": {
        "@type": "Person",
        "name": "作者名",
        "url": "https://example.com/author/name"
    },
    "publisher": {
        "@type": "Organization",
        "name": "技术博客",
        "logo": {
            "@type": "ImageObject",
            "url": "https://example.com/logo.png"
        }
    },
    "datePublished": "2024-12-07",
    "dateModified": "2024-12-07",
    "mainEntityOfPage": {
        "@type": "WebPage",
        "@id": "https://example.com/blog/seo-guide"
    }
}
</script>

常见结构化数据类型

面包屑导航

<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [
        {
            "@type": "ListItem",
            "position": 1,
            "name": "首页",
            "item": "https://example.com"
        },
        {
            "@type": "ListItem",
            "position": 2,
            "name": "博客",
            "item": "https://example.com/blog"
        },
        {
            "@type": "ListItem",
            "position": 3,
            "name": "SEO 指南",
            "item": "https://example.com/blog/seo-guide"
        }
    ]
}
</script>

FAQ 常见问题

<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@type": "FAQPage",
    "mainEntity": [
        {
            "@type": "Question",
            "name": "什么是 SEO?",
            "acceptedAnswer": {
                "@type": "Answer",
                "text": "SEO(Search Engine Optimization)是搜索引擎优化的缩写,指通过优化网站来提高在搜索引擎中的排名。"
            }
        },
        {
            "@type": "Question",
            "name": "SEO 需要多长时间见效?",
            "acceptedAnswer": {
                "@type": "Answer",
                "text": "通常需要 3-6 个月才能看到明显效果,具体取决于竞争程度和优化力度。"
            }
        }
    ]
}
</script>

网站搜索框

<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@type": "WebSite",
    "name": "技术博客",
    "url": "https://example.com",
    "potentialAction": {
        "@type": "SearchAction",
        "target": "https://example.com/search?q={search_term_string}",
        "query-input": "required name=search_term_string"
    }
}
</script>

Go 后端生成结构化数据

// models/structured_data.go
package models

type ArticleLD struct {
    Context         string      `json:"@context"`
    Type            string      `json:"@type"`
    Headline        string      `json:"headline"`
    Description     string      `json:"description"`
    Image           string      `json:"image"`
    Author          PersonLD    `json:"author"`
    Publisher       OrgLD       `json:"publisher"`
    DatePublished   string      `json:"datePublished"`
    DateModified    string      `json:"dateModified"`
    MainEntityOfPage PageLD     `json:"mainEntityOfPage"`
}

type PersonLD struct {
    Type string `json:"@type"`
    Name string `json:"name"`
    URL  string `json:"url,omitempty"`
}

type OrgLD struct {
    Type string   `json:"@type"`
    Name string   `json:"name"`
    Logo ImageLD  `json:"logo"`
}

type ImageLD struct {
    Type string `json:"@type"`
    URL  string `json:"url"`
}

type PageLD struct {
    Type string `json:"@type"`
    ID   string `json:"@id"`
}

// 生成文章结构化数据
func GenerateArticleLD(post *Post, baseURL string) *ArticleLD {
    return &ArticleLD{
        Context:     "https://schema.org",
        Type:        "Article",
        Headline:    post.Title,
        Description: post.Summary,
        Image:       baseURL + post.CoverImage,
        Author: PersonLD{
            Type: "Person",
            Name: post.Author.Name,
            URL:  baseURL + "/author/" + post.Author.Slug,
        },
        Publisher: OrgLD{
            Type: "Organization",
            Name: "技术博客",
            Logo: ImageLD{
                Type: "ImageObject",
                URL:  baseURL + "/logo.png",
            },
        },
        DatePublished: post.CreatedAt.Format("2006-01-02"),
        DateModified:  post.UpdatedAt.Format("2006-01-02"),
        MainEntityOfPage: PageLD{
            Type: "WebPage",
            ID:   baseURL + "/post/" + post.Slug,
        },
    }
}

性能优化

网站性能是 Google 核心排名因素之一,特别是 Core Web Vitals 指标。

Core Web Vitals

┌─────────────────────────────────────────────────────────────────┐
│                    Core Web Vitals                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  LCP (Largest Contentful Paint) - 最大内容绘制           │   │
│   │  衡量加载性能,应在 2.5 秒内完成                         │   │
│   │                                                         │   │
│   │  ✅ 良好: < 2.5s   ⚠️ 需改进: 2.5-4s   ❌ 差: > 4s      │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  INP (Interaction to Next Paint) - 交互到下一次绘制      │   │
│   │  衡量交互响应性,应在 200ms 内完成                       │   │
│   │                                                         │   │
│   │  ✅ 良好: < 200ms  ⚠️ 需改进: 200-500ms  ❌ 差: > 500ms │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  CLS (Cumulative Layout Shift) - 累积布局偏移            │   │
│   │  衡量视觉稳定性,应保持在 0.1 以下                       │   │
│   │                                                         │   │
│   │  ✅ 良好: < 0.1    ⚠️ 需改进: 0.1-0.25   ❌ 差: > 0.25  │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

优化策略

1. 优化 LCP

<!-- 预加载关键资源 -->
<link rel="preload" href="/fonts/main.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="/images/hero.webp" as="image">

<!-- 优化首屏图片 -->
<img 
    src="/images/hero.webp" 
    alt="Hero image"
    fetchpriority="high"
    loading="eager"
>

<!-- 内联关键 CSS -->
<style>
    /* 首屏关键样式 */
    .hero { ... }
</style>

2. 优化 INP

// 使用 requestIdleCallback 处理非关键任务
function processData(data) {
    requestIdleCallback(() => {
        // 处理数据
    });
}

// 长任务分片处理
async function processLargeArray(items) {
    const CHUNK_SIZE = 100;
    for (let i = 0; i < items.length; i += CHUNK_SIZE) {
        const chunk = items.slice(i, i + CHUNK_SIZE);
        await processChunk(chunk);
        // 让出主线程
        await new Promise(resolve => setTimeout(resolve, 0));
    }
}

// 使用 Web Worker 处理计算密集型任务
const worker = new Worker('/js/heavy-task.js');
worker.postMessage({ data });

3. 优化 CLS

/* 为图片预留空间 */
img, video {
    aspect-ratio: 16 / 9;
    width: 100%;
    height: auto;
}

/* 避免动态插入内容导致布局偏移 */
.ad-container {
    min-height: 250px;
}

/* 字体加载优化 */
@font-face {
    font-family: 'CustomFont';
    src: url('/fonts/custom.woff2') format('woff2');
    font-display: swap;
    /* 或使用 optional 避免布局偏移 */
}

资源优化

优化项方法
图片WebP/AVIF 格式、响应式图片、懒加载
JavaScript代码分割、Tree Shaking、延迟加载
CSS关键 CSS 内联、移除未使用样式
字体子集化、preload、font-display
缓存Service Worker、HTTP 缓存头

移动端 SEO

Google 采用移动优先索引(Mobile-First Indexing),移动端体验至关重要。

响应式设计

<!-- 视口设置 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
/* 响应式断点 */
/* 移动端优先 */
.container {
    width: 100%;
    padding: 0 16px;
}

/* 平板 */
@media (min-width: 768px) {
    .container {
        max-width: 720px;
        margin: 0 auto;
    }
}

/* 桌面 */
@media (min-width: 1024px) {
    .container {
        max-width: 960px;
    }
}

/* 大屏 */
@media (min-width: 1280px) {
    .container {
        max-width: 1200px;
    }
}

移动端易用性

┌─────────────────────────────────────────────────────────────────┐
│                    移动端优化检查清单                            │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ✅ 触摸目标大小                                               │
│      • 按钮/链接至少 48x48 像素                                 │
│      • 元素间距足够,避免误触                                   │
│                                                                 │
│   ✅ 字体大小                                                   │
│      • 正文至少 16px                                            │
│      • 行高 1.5-1.8                                             │
│                                                                 │
│   ✅ 视口设置                                                   │
│      • 正确的 viewport meta 标签                                │
│      • 不禁用缩放                                               │
│                                                                 │
│   ✅ 内容可见性                                                 │
│      • 不使用 Flash                                             │
│      • 弹窗不遮挡主要内容                                       │
│      • 横向不需要滚动                                           │
│                                                                 │
│   ✅ 页面速度                                                   │
│      • 在 3G 网络下可用                                         │
│      • 资源优化压缩                                             │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

AMP(加速移动页面)

虽然 AMP 不再是排名因素,但对于新闻类网站仍有价值:

<!doctype html>
<html amp lang="zh-CN">
<head>
    <meta charset="utf-8">
    <script async src="https://cdn.ampproject.org/v0.js"></script>
    <title>文章标题</title>
    <link rel="canonical" href="https://example.com/article">
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
    <style amp-boilerplate>...</style>
    <style amp-custom>
        /* 自定义样式 */
    </style>
</head>
<body>
    <amp-img src="image.jpg" width="800" height="600" layout="responsive"></amp-img>
</body>
</html>

SEO 工具与监控

Google Search Console

Google 官方提供的免费 SEO 工具,功能包括:

功能说明
性能报告查看搜索查询、点击量、排名
索引覆盖检查页面索引状态
URL 检查测试单个 URL 的索引情况
站点地图提交和监控 sitemap
移动易用性检查移动端问题
Core Web Vitals性能指标报告

设置 Google Search Console

  1. 访问 Google Search Console
  2. 添加网站资源
  3. 验证所有权(推荐 DNS 验证)
<!-- HTML 标签验证 -->
<meta name="google-site-verification" content="your-verification-code">

其他 SEO 工具

工具用途价格
Google Analytics流量分析免费
Lighthouse性能/SEO 审计免费
Ahrefs外链分析、关键词研究付费
SEMrush综合 SEO 工具付费
Screaming Frog网站爬取分析免费/付费
PageSpeed Insights页面速度测试免费

监控脚本示例

// monitoring/seo_checker.go
package monitoring

import (
    "encoding/json"
    "fmt"
    "net/http"
    "time"
)

type SEOReport struct {
    URL           string    `json:"url"`
    StatusCode    int       `json:"status_code"`
    Title         string    `json:"title"`
    Description   string    `json:"description"`
    H1Count       int       `json:"h1_count"`
    HasCanonical  bool      `json:"has_canonical"`
    LoadTime      float64   `json:"load_time_ms"`
    CheckedAt     time.Time `json:"checked_at"`
    Issues        []string  `json:"issues"`
}

// CheckURL 检查 URL 的 SEO 状态
func CheckURL(url string) (*SEOReport, error) {
    start := time.Now()
    
    resp, err := http.Get(url)
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()
    
    loadTime := time.Since(start).Milliseconds()
    
    report := &SEOReport{
        URL:        url,
        StatusCode: resp.StatusCode,
        LoadTime:   float64(loadTime),
        CheckedAt:  time.Now(),
        Issues:     []string{},
    }
    
    // 解析 HTML 并检查 SEO 元素
    // ... 解析逻辑
    
    // 检查问题
    if report.Title == "" {
        report.Issues = append(report.Issues, "缺少 title 标签")
    }
    if len(report.Title) > 60 {
        report.Issues = append(report.Issues, "title 过长")
    }
    if report.Description == "" {
        report.Issues = append(report.Issues, "缺少 meta description")
    }
    if report.H1Count == 0 {
        report.Issues = append(report.Issues, "缺少 H1 标签")
    }
    if report.H1Count > 1 {
        report.Issues = append(report.Issues, "存在多个 H1 标签")
    }
    if !report.HasCanonical {
        report.Issues = append(report.Issues, "缺少 canonical 标签")
    }
    if report.LoadTime > 3000 {
        report.Issues = append(report.Issues, "页面加载时间过长")
    }
    
    return report, nil
}

常见问题与解决方案

问题排查表

问题可能原因解决方案
页面未被索引robots.txt 阻止、noindex 标签、重复内容检查 robots.txt,移除 noindex,设置 canonical
排名下降算法更新、技术问题、内容质量检查 Search Console 错误,更新内容
爬虫抓取减少服务器响应慢、死链接多优化性能,修复 404 页面
移动端问题响应式问题、触摸目标过小使用移动端友好测试工具检查
结构化数据错误语法错误、缺少必填字段使用 Rich Results Test 验证

常见 SEO 误区

┌─────────────────────────────────────────────────────────────────┐
│                    SEO 常见误区                                  │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ❌ 误区1:关键词密度是排名因素                                 │
│   ✅ 事实:自然写作比刻意堆砌关键词更重要                        │
│                                                                 │
│   ❌ 误区2:Meta keywords 很重要                                 │
│   ✅ 事实:Google 早已不使用 meta keywords                       │
│                                                                 │
│   ❌ 误区3:外链越多越好                                         │
│   ✅ 事实:质量比数量更重要,垃圾外链会受惩罚                    │
│                                                                 │
│   ❌ 误区4:SEO 是一次性工作                                     │
│   ✅ 事实:SEO 需要持续优化和监控                                │
│                                                                 │
│   ❌ 误区5:新网站很快就能获得好排名                              │
│   ✅ 事实:建立权威需要时间,通常需要 6-12 个月                  │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

参考资料

  1. Google SEO 入门指南
  2. Google Search Central Blog
  3. Web.dev - SEO 最佳实践
  4. Schema.org - 结构化数据
  5. Core Web Vitals
  6. Ahrefs SEO 博客
  7. Moz SEO 学习中心
  8. Google Search Console 帮助

评论 (0)

请先登录后再发表评论

暂无评论,来发表第一条评论吧!