处理404错误与重定向到主页的最佳方法 [英] Best way to handle 404 error with redirect to homepage

查看:1062
本文介绍了处理404错误与重定向到主页的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查,读了很多,但还是没能找到一种方法,来实现我的目标。现在,我甚至觉得这是不可能的。是吗?

I've searched, read a lot, but still couldn't find a way, to achieve my goal. Now I even think it is impossible. Is it?

我要404错误,处理不当,所以我有的ErrorDocument 404 /404.php 在.htaccess文件中,我要发出正确的头搜索引擎标题(HTTP / 1.1 404未找​​到)忘记页面,同时我想我的访问者到 /index.php?$c$c=404 这样我就可以告诉他有关他在找什么的,是不可用,但还是让他在网站上。

I want 404 errors to handled properly so I have ErrorDocument 404 /404.php in the .htaccess file, and I want to send out the right header to search engines header("HTTP/1.1 404 Not Found") to forget that page, also I want to redirect my visitor to /index.php?code=404 so I can inform him about that what he was looking for is not available, but still keep him on the site.

现在看来一切都很好,直到我想将用户重定向:那么服务器发出一个302的消息,这将覆盖我的previous 404头,因此,虽然我可以将用户发送到需要的地方,我还是不能告诉searchbots该地址是错误的。

Now it seems everything is fine, until I want to redirect the user: then the server sends out a 302 message, which overwrites my previous 404 header, so while I can send the user to the desired place, I still can't tell the searchbots that the address is wrong.

如果我添加一个包括:(的index.php?code = 404)标题行,它只是将无法正常工作后,服务器说,文件不存在。

If I add a include("index.php?code=404") after the header line it just won't work, the server says that file does not exist.

我应该怎么办?

推荐答案

尝试是这样的:

<?php

    header("HTTP/1.0 404 Not Found");

    echo '<html>
            <head>
                <meta http-equiv="Refresh" content="0;url=http://www.mysite.com/index.php?code=404" />
            </head><body></body>
          </html>';


?>

我使用的是重定向,否则你得到的头已经发送的错误。

I'm using a meta redirect because otherwise you get an error that headers have already been sent.

我使用招测试了在本地主机上,并得到了适当的404响应,然后正确地重定向到不同的页面成功。

I tested this on localhost using Fiddler, and got a proper 404 response, and then was correctly redirected to a different page successfully.

这篇关于处理404错误与重定向到主页的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆