301重定向是一种永久性重定向技术,用于将一个URL的流量和权重永久转移到另一个URL,在网站改版、域名更换、URL结构调整等场景中,正确设置301重定向对于维持搜索引擎排名(SEO)和用户体验至关重要,以下是详细的301重定向制作方法,涵盖不同服务器环境和常见场景。

301重定向的重要性
- SEO权重传递:搜索引擎会将原域名的权重和链接价值传递到新域名,避免排名下降。
- 用户体验优化:确保用户访问旧URL时自动跳转到正确页面,减少404错误。
- 规范化URL:统一带www和不带www的域名,或修复HTTP/HTTPS混用问题。
常见服务器环境下的301重定向设置方法
Apache服务器
Apache通过.htaccess文件实现重定向,需确保已启用mod_rewrite模块。
- 单页面重定向:
Redirect 301 /old-page.html https://www.example.com/new-page.html
- 整个域名重定向:
RewriteEngine On RewriteRule ^(.*)$ https://www.new-domain.com/$1 [R=301,L]
- 带www与不带www的统一(强制跳转到www):
RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Nginx服务器
Nginx通过配置文件(如nginx.conf或站点配置文件)实现重定向。
- 单页面重定向:
location /old-page.html { return 301 https://www.example.com/new-page.html; } - 整个域名重定向:
server { listen 80; server_name old-domain.com; return 301 https://www.new-domain.com$request_uri; } - HTTP跳转HTTPS:
server { listen 80; server_name example.com www.example.com; return 301 https://$host$request_uri; }
IIS服务器
IIS通过“URL重写”模块实现,需安装该模块(默认未安装)。
- 单页面重定向:
在web.config中添加:<configuration> <system.webServer> <rewrite> <rules> <rule name="Redirect Old Page" stopProcessing="true"> <match url="^old-page.html$" /> <action type="Redirect" url="https://www.example.com/new-page.html" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration> - 域名重定向:
<rule name="Redirect Domain" stopProcessing="true"> <match url="(.*)" /> <action type="Redirect" url="https://www.new-domain.com/{R:1}" redirectType="Permanent" /> </rule>
WordPress网站
- 插件法:安装“Redirection”或“Yoast SEO”插件,在后台设置重定向规则。
- 代码法:在主题的
functions.php中添加:add_action('template_redirect', 'redirect_old_page'); function redirect_old_page() { if (is_page('old-page')) { wp_redirect('https://www.example.com/new-page', 301); exit; } }
HTML页面(临时方案)
若无法修改服务器配置,可在HTML中通过<meta>标签实现客户端跳转(非301,仅作临时参考):

<meta http-equiv="refresh" content="0; url=https://www.example.com/new-page.html">
注意:此方法无法传递SEO权重,仅适用于无法配置服务器的场景。
301重定向的注意事项
- 测试重定向:使用工具(如Redirect Checker、curl命令)验证重定向是否正确生效。
curl -I https://old-domain.com/old-page.html
检查响应头是否包含
HTTP/1.1 301 Moved Permanently。 - 避免重定向链:不要设置A→B→C的多级重定向,直接指向最终URL。
- 保留路径参数:使用
$1或$request_uri确保动态路径(如/blog/123)正确传递。 - HTTPS与HTTP:确保重定向目标使用HTTPS,避免混合内容问题。
常见场景重定向规则对比
| 场景 | Apache (.htaccess) | Nginx (配置文件) |
|---|---|---|
| 域名更换 | RewriteRule ^(.*)$ https://new.com/$1 [L,R=301] |
return 301 https://new.com$request_uri; |
| HTTP转HTTPS | RewriteCond %{HTTPS} off + 重定向规则 |
return 301 https://$host$request_uri; |
| 移除www | RewriteCond %{HTTP_HOST} ^www\. [NC] + 规则 |
if ($host ~* ^www\.(.+)) { ... } |
相关问答FAQs
Q1:301重定向和302重定向有什么区别?
A:301是永久重定向,搜索引擎会永久更新索引并传递权重;302是临时重定向,搜索引擎会保留原URL索引,不传递权重,适用于临时维护或A/B测试场景。
Q2:设置301重定向后,多久搜索引擎会更新索引?
A:通常需要几天到几周不等,Google可能需要1-2周完全更新,而百度可能需要更长时间,建议通过Google Search Console提交“更改地址”工具加速索引更新。

