PHP 301 重定向想法

发布于 2024-08-15 12:49:34 字数 1109 浏览 4 评论 0原文

我之前发布了这个

301静态HTML重定向到动态PHP页面

但是有一个新想法,并且我想知道是否存在任何问题,为什么我不应该这样做...

如果有人尝试访问我们网站上的死页面,例如:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

前事休说 2024-08-22 12:49:34

嗯,是的。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文