php的自定义错误页

php的自定义错误页

本文介绍了作为index.php的自定义错误页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Apache 2.2.X和PHP 5.2.X(安装为Apache模块),以建立一个新的网站,我想了解我是如何试图处理服务器错误您的建议。
我想使用我的主页上的同一个文件(的index.php)来显示自定义错误消息。
这是我的.htaccess设置:

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.
I was thinking about using the same file of my homepage (/index.php) to show custom error messages.This is my .htaccess setup:

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

现在,在我的index.php文件,我有一些code,看起来像这样:

Now, in my index.php file I have some code that looks like this:

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

一切工作就像一个魅力
好了,一切,除了一件事,我无法修复:如果我强制Apache在一个500状态code反应(例如,插入恶意code进入我的.htaccess),我不是被重定向到/index.php?error=500,但我得到了默认的500错误页面。与其他状态code(例如,403或404)我的配置工作万无一失。

但现在我有一个疑问,我开始认为,这将是更好的用另一个页面来处理错误(例如,/error.php)。
DrawHomepage()需要robots元标记设置为指数,遵循,而DrawErrorPage()需要将其设置为NOINDEX,nofollow的。对?所以......如果一个网络爬虫会得到一个错误响应访问我的主页,第一次会发生什么?如果一个网络爬虫得到200访问我的主页,第一次会发生什么,但500访问过它一个月以后呢?如果我把我的robots元标记会发生什么指数,遵循即使我显示错误?

有一种解决方法,解决办法,这个问题?你会怎么做?

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. With any other status code (for example, 403 or 404) my configuration works absolutely perfectly.

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").
"DrawHomepage()" needs to set "robots" meta tag to "index, follow", while "DrawErrorPage()" needs to set it to "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? 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?

非常感谢!

推荐答案

通常,如果有一个5​​00状态code,那么阿帕奇已经乱七八糟的东西和的它不能运行索引。 php文件,造成的其他的500状态code。阿帕奇持续了几个迭代这个错误环终于说,没有更多的回路和发送它自己的错误页面之前。

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. Apache continues this error loop for a few iterations before it finally says "no more loops" and sending its own error page.

可以显示500状态code自定义页面的唯一真正安全的方法是使用纯文本或使用一个基本的.html或文件的.shtml不尝试访问您的服务器上的其他东西,所以你不保持在页面加载引发更多500状态codeS。

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,它只会暂时忽略页面。一个500 code是可恢复的,它并不一定意味着有就是有没有网页,只是服务器的时刻搞砸了。该机器人是聪明,能确定什么错误codeS的意思,只要在页面总是发送的状态code在页​​头。

Usually if a crawler encounters a 500, it will just ignore the page temporarily. 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. 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.

记住,如果你使用PHP文件作为错误文档,你需要使用PHP内头功能,以确保适当页检测到重新发送HTTP状态code,像这样:

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:

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

这篇关于作为index.php的自定义错误页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:49