HTTP 状态码 301(Moved Permanently) 和 302(Found) 都表示网页重定向,但它们的用途和 SEO 影响不同。这里我们对比看看两者的设置方法。
第一、301和302的区别
特性 | 301 重定向 | 302 重定向 |
---|---|---|
类型 | 永久重定向 | 临时重定向 |
SEO 影响 | 搜索引擎会将权重(PageRank)转移到新 URL | 搜索引擎不会转移权重,仍认为原 URL 有效 |
浏览器缓存 | 浏览器会缓存重定向,后续直接访问新 URL | 浏览器不会缓存,每次都会请求原 URL |
适用场景 | 网站永久迁移、域名更换、URL 结构调整 | 临时维护、A/B 测试、短链接跳转 |
第二、设置方法
Apache 301永久重定向
# 将旧 URL 重定向到新 URL(永久)
Redirect 301 /old-page.html https://example.com/new-page.html
# 或使用 RewriteRule(更灵活)
RewriteEngine On
RewriteRule ^old-page\.html$ https://example.com/new-page.html [R=301,L]
Apache 302临时重定向
# 临时重定向
Redirect 302 /temp-page.html https://example.com/new-temp-page.html
# 或使用 RewriteRule
RewriteEngine On
RewriteRule ^temp-page\.html$ https://example.com/new-temp-page.html [R=302,L]
Nginx 永久重定向
server {
listen 80;
server_name example.com;
# 301 重定向
location /old-page.html {
return 301 https://example.com/new-page.html;
}
}
Nginx 302临时重定向
server {
listen 80;
server_name example.com;
# 302 重定向
location /temp-page.html {
return 302 https://example.com/new-temp-page.html;
}
}