简体   繁体   English

Index.php作为自定义错误页面

[英]Index.php as custom error page

I'm using Apache 2.2.X and PHP 5.2.X (installed as Apache module) to build a new website and I would like to read your suggestions about how I'm trying to handle server errors. 我正在使用Apache 2.2.X和PHP 5.2.X(作为Apache模块安装)来构建新网站,并且我想阅读您有关如何处理服务器错误的建议。
I was thinking about using the same file of my homepage (/index.php) to show custom error messages. 我正在考虑使用主页(/index.php)的相同文件显示自定义错误消息。 This is my .htaccess setup: 这是我的.htaccess设置:

ErrorDocument 400 /index.php?error=400
ErrorDocument 401 /index.php?error=401
ErrorDocument 403 /index.php?error=403
ErrorDocument 404 /index.php?error=404
ErrorDocument 500 /index.php?error=500

Now, in my index.php file I have some code that looks like this: 现在,在我的index.php文件中,我有一些看起来像这样的代码:

if (isset($_GET['error']))
    DrawErrorPage($_GET['error']);
else
    DrawHomepage();

Everything works like a charm. 一切都像魅力。
Well, everything except one thing that I can't fix: if I force Apache to respond with a 500 status code (for example, inserting malformed code into my .htaccess), I'm not being redirected to "/index.php?error=500", but I get the default 500 error page instead. 好吧,除了一件我无法解决的事情以外,所有一切:如果我强迫Apache用500状态代码进行响应(例如,将格式错误的代码插入到我的.htaccess中),我是否不会被重定向到“ /index.php? error = 500“,但是我得到了默认的500错误页面。 With any other status code (for example, 403 or 404) my configuration works absolutely perfectly. 使用任何其他状态代码(例如403或404),我的配置都可以完美地工作。

But now I've a doubt and I'm starting to think that it would be better to use another page to handle errors (for example, "/error.php"). 但是现在我有一个疑问,我开始认为最好使用另一个页面来处理错误(例如,“ / error.php”)。
"DrawHomepage()" needs to set "robots" meta tag to "index, follow", while "DrawErrorPage()" needs to set it to "noindex, nofollow". “ DrawHomepage()”需要将“机器人”元标记设置为“索引,跟随”,而“ DrawErrorPage()”需要将其设置为“ noindex,nofollow”。 Right? 对? So... what would happen if a web crawler gets an error response visiting my homepage for the first time? 那么...如果网络爬虫第一次收到访问我的主页的错误响应会怎样? What would happen if a web crawler gets 200 visiting my homepage for the first time, but a 500 visiting it a month later? 如果某个网络爬虫第一次使200个访问我的主页,但一个月后又有500个访问我的主页,将会发生什么? And what would happen if I keep my "robots" meta tag to "index, follow" even if I'm showing errors? 如果我在显示错误的情况下仍将“机器人”元标记保持为“索引,跟随”,将会发生什么情况?

Is there a workaround, a solution, for this problem? 是否有解决此问题的解决方法? What would you do? 你会怎么做?

Many thanks! 非常感谢!

Usually if there is a 500 status code then Apache has messed something up and it can't run your index.php file, resulting in another 500 status code. 通常,如果状态码为500,则Apache搞砸了一些东西, 它无法运行您的index.php文件,从而产生了500状态码。 Apache continues this error loop for a few iterations before it finally says "no more loops" and sending its own error page. Apache继续执行此错误循环几次迭代,然后最终说“不再循环”并发送自己的错误页面。

The only really safe way to display a custom page for a 500 status code is to use plain text or use a basic .html or .shtml file that doesn't try to access other things on your server, so you don't keep triggering more 500 status codes in the page load. 显示500状态代码的自定义页面的唯一真正安全的方法是使用纯文本或使用基本的.html或.shtml文件,它们不会尝试访问服务器上的其他内容,因此您不会一直触发页面加载中有500多个状态代码。

Usually if a crawler encounters a 500, it will just ignore the page temporarily. 通常,如果某个搜寻器遇到500,它只会暂时忽略该页面。 A 500 code is recoverable, it doesn't necessarily mean there is no page there, just that the server is messed up at the moment. 500个代码是可恢复的,这不一定意味着那里没有页面,只是此时服务器已混乱。 The bots are smart and can determine what error codes mean what, as long as the page is always sending the status code in the page header. 僵尸程序很聪明,只要页面始终在页面标题中发送状态代码,它们就可以确定什么错误代码意味着什么。

Remember, if you use a PHP file as your error document, you need to resend the HTTP status code using the header function inside PHP to ensure proper page detection, like so: 请记住,如果将PHP文件用作错误文档,则需要使用PHP内的标头功能重新发送HTTP状态代码,以确保正确的页面检测,如下所示:

header("HTTP/1.1 404 Page Not Found");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM