php实现301
实现301重定向的方法
在PHP中实现301重定向(永久重定向)可以通过以下几种方式完成:
使用header函数
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://www.example.com/new-page");
exit();
对于旧版PHP(5.4以下)
header("Status: 301 Moved Permanently");
header("Location: https://www.example.com/new-page");
exit();
在.htaccess文件中实现

如果使用Apache服务器,可以在.htaccess文件中添加规则:
Redirect 301 /old-page.html https://www.example.com/new-page.html
或使用mod_rewrite:
RewriteEngine On
RewriteRule ^old-page\.html$ https://www.example.com/new-page.html [R=301,L]
注意事项

- 必须在输出任何内容之前调用header()函数
- 重定向后建议使用exit()或die()终止脚本执行
- 确保URL是完整的绝对路径(包含http://或https://)
- 301重定向会影响SEO,搜索引擎会将旧页面的权重传递给新页面
检测重定向是否生效
可以使用curl命令检查响应头:
curl -I https://www.example.com/old-page
应看到类似以下响应:
HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/new-page






