重定向

域名基础
自动将访问者从一个URL转发到另一个URL的技术。
← 返回词汇表

什么是重定向?

重定向是一种将访问者和搜索引擎从一个 URL 自动转发到另一个 URL 的技术。当用户请求原始 URL 时,服务器会返回指示其前往其他目标的响应。重定向对于网站维护、域名迁移、URL 结构调整,以及在内容位置变化后保留 SEO 价值都至关重要。

HTTP 重定向类型

301 - 永久重定向

表示页面已永久移动:

HTTP/1.1 301 Moved Permanently

Location: https://newsite.com/page

302 - 临时重定向(Found)

表示页面暂时移动:

HTTP/1.1 302 Found

Location: https://temporary-url.com/page

307 - 临时重定向(Strict)

保留请求方法的 HTTP/1.1 版本:

308 - 永久重定向(Strict)

保留请求方法的 HTTP/1.1 永久重定向:

常见重定向使用场景

域名整合

将所有变体重定向到主域名:

example.com → www.example.com (or vice versa)

http:// → https://

oldbrand.com → newbrand.com

URL 结构变更

重新组织网站架构时:

/old-page/ → /new-page/

/category/page → /new-category/page

/blog/2020/post → /articles/post

内容迁移

在平台或域名之间移动内容:

blog.example.com/post → example.com/blog/post

oldsite.com/* → newsite.com/*

强制使用 HTTPS

保护所有流量:

http://example.com → https://example.com

实现方式

服务器配置(Apache)

# .htaccess

Redirect 301 /old-page https://example.com/new-page

# Multiple redirects with RewriteRule

RewriteEngine On

RewriteRule ^old-path/(.*)$ /new-path/$1 [R=301,L]

# Force HTTPS

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

服务器配置(Nginx)

# Single redirect

location /old-page {

return 301 https://example.com/new-page;

}

# Pattern-based redirect

location ~* ^/old-path/(.*)$ {

return 301 /new-path/$1;

}

# Force HTTPS

server {

listen 80;

server_name example.com;

return 301 https://example.com$request_uri;

}

JavaScript 重定向(客户端)

// Immediate redirect

window.location.href = "https://example.com/new-page";

// Delayed redirect

setTimeout(() => {

window.location.replace("https://example.com/new-page");

}, 3000);

Meta Refresh(HTML)

<meta http-equiv="refresh" content="0;url=https://example.com/new-page">

*注意:不建议将其用于 SEO*

SEO 考量

最佳实践

1. 永久变更使用 301:传递排名信号

2. 避免重定向链:A→B→C→D 会降低抓取效率

3. 更新内部链接:指向最终目标

4. 监控重定向性能:在分析工具中跟踪

5. 设置正确的规范链接:与重定向配合使用

重定向对 SEO 的影响

重定向类型链接权益传递索引更新
301约 90-99%
302极少
Meta refresh不固定缓慢
JavaScript有限不可靠

常见重定向问题

重定向链

多个连续的重定向:

A → B → C → D (Bad: 4 hops)

A → D (Good: Direct)

重定向循环

无限重定向循环:

A → B → A → B → ... (Error: ERR_TOO_MANY_REDIRECTS)

混合内容重定向

HTTPS 页面重定向到 HTTP(或反之,但没有正确的链路)

正确实施重定向对于在任何 URL 或域名发生变化期间维持用户体验和搜索引擎排名至关重要。

将知识付诸实践

使用 DomScan 的 API 检查域名可用性、健康状态等。