PHP 301 重定向想法
我之前发布了这个
但是有一个新想法,并且我想知道是否存在任何问题,为什么我不应该这样做...
如果有人尝试访问我们网站上的死页面,例如:
(domain)/somepage.html
现在存在于此处:
(domain)/dynamic.php? id=1
它失败并转到自定义错误 404 页面 (/404.php)
如果我查看 $_SERVER['REDIRECT_URL']
变量,我可以看到他们试图去的地方。我的想法是在 404.php 页面顶部添加一个 include 来检查该值,如果它在我要重定向的项目列表中,则使用 PHP 执行 301。
类似这样的事情...
// -- php include at top of 404.php page
switch(trim($_SERVER['REDIRECT_URL'])){
case "/oldpage.html" : $location = "/dynamic.php?id=1"; break;
case "/oldpage2.html" : $location = "/dynamic.php?id=2"; break;
}
if(isset($location) && trim($location) != ''){
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
exit(0);
}
// -- end of php include
这给了我一个点可以输入我在谷歌网站管理员工具中看到的所有链接,这些链接位于博客条目等中,但现在已经死了。
谢谢
I posted this earlier
301 Redirect of Static HTML to Dynamic PHP Page
But have a new idea, and am wondering if there are any issues why I should NOT do this...
If someone tries to go to a dead page on our site like:
(domain)/somepage.html
That now exists here:
(domain)/dynamic.php?id=1
It fails and goes to a custom Error 404 page (/404.php)
If I look at the $_SERVER['REDIRECT_URL']
variable, I can see where they were trying to go. My idea is to add an include at the top of the 404.php page to check this value, and if it's in my list of items to redirect, then to use PHP to do the 301.
Something like this...
// -- php include at top of 404.php page
switch(trim($_SERVER['REDIRECT_URL'])){
case "/oldpage.html" : $location = "/dynamic.php?id=1"; break;
case "/oldpage2.html" : $location = "/dynamic.php?id=2"; break;
}
if(isset($location) && trim($location) != ''){
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);
exit(0);
}
// -- end of php include
This gives me a single point to enter in all the links I see in the google webmaster tools that are in blog entries, etc. that are now dead.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,是的。 301 伴随着
Location
标头,是对您可以明确识别为已移动的请求的正确响应。Well, yes. 301, accompanied by a
Location
header, is the proper response for a request that you can positively identify as being moved.