在 cPanel 中创建自定义 PHP 错误页面,而不将它们从引发错误的页面重定向
Create custom PHP error pages in cPanel without redirecting them away from the page that has thrown the error
我正在尝试让我的网络服务器出现自定义错误 documents/pages 但是 其中包含 PHP
代码,而不会将它们重定向到页面之外引发了错误。
假设我们正在导航到 http://website.com/pages/1
并且它会抛出错误 500,根据 默认值 该页面将只是一个带有文本“的空白页面” Error 500 (Internal Server Error)
" 看起来像这样:
从上面可以看出,它没有重定向到抛出错误的页面。我希望此页面看起来 "a part of the website" 但其中包含 PHP 内容。
我无法通过编辑您在下面看到的页面在 cPanel 的错误页面中包含 PHP 内容:
如果我要编辑上面的错误 500 页面,下面的内容就是 http://website.com/pages/1
会重定向到 http://website.com/500.shtml
然后依次重定向到 http://website.com/500.php
我不想要它重定向,否则这意味着刷新页面本质上只会刷新500.php页面而不是刷新/pages/1
<script language="javascript">
window.location.href = "http://www.website.com/500.php"
</script>
<meta http-equiv="refresh" content="0;URL='http://website.com/500.php'" />
会存在完全相同的问题,只是它不是 2 个链重定向,而是使用 .htaccess 文件中的以下内容只是 1 个重定向
ErrorDocument 400 http://website.com/400.php
ErrorDocument 401 http://website.com/401.php
ErrorDocument 403 http://website.com/403.php
ErrorDocument 404 http://website.com/404.php
ErrorDocument 500 http://website.com/500.php
Current Result: Redirecting to /500.php
Expected Result: Displaying 500.php in/on http://website.com/pages/1 without redirect
我怎样才能拥有包含 php 内容的自定义错误页面而不使其重定向到引发错误的页面?
有没有办法通过 root 执行此操作(我可以让我的主机做一些事情吗?如果可以的话?)
如果您在 .htaccess 文件中添加类似这样的内容,它应该只显示错误页面而无需重定向。我从来没有像你在错误页面的例子中那样使用绝对路径,所以我不确定它会导致什么样的行为
ErrorDocument 404 /404.php
正如其他人的回答,这应该有效:
ErrorDocument 500 /500.php
如果您没有指定完整的 URL,它只会显示页面(内部重定向),如果您指定了完整的 URL,那么它会向客户端发出重定向。这是 Apache 在这里描述的:http://httpd.apache.org/docs/2.4/mod/core.html#errordocument
Note that when you specify an ErrorDocument that points to a remote
URL (ie. anything with a method such as http in front of it), Apache
HTTP Server will send a redirect to the client to tell it where to
find the document, even if the document ends up being on the same
server.
以上内容对您不起作用的事实意味着以下几种情况之一:
- 你做错了,尽管你声称不是,但仍然指定了完整的 URL。
- Cpanel 正在添加完整的 URL(不确定您是如何编辑 .htaccess 文件的,或者是否是通过上面的 GUI 完成的)。
- 您的 .htaccess 文件被忽略(可能是因为您正在 GUI 中设置值,或者可能是因为 FileInfo 未 在服务器级别设置,因此无法在 . htaccess 文件(参见此处:http://httpd.apache.org/docs/2.4/custom-error.html),在这种情况下,它必须从某处的其他配置中获取完整的 URL 本身。
- 500.php 脚本正在自行进行重定向(如果保留日志并查看是否返回一页或两页,从开发人员工具中应该很明显)。
- Apache 中存在错误(我和其他人都不知道)。
- 魔法
你能检查一下那些东西吗?
可能还有助于了解您使用的 Apache 版本?
您是否也尝试过一个普通的 500.html 文件,看看它是否可以在不重定向的情况下工作?从理论上讲,500.php 文件也应该没问题,但缩小范围会很有趣(并且还会显示对 500.html 的更改是否被拾取和使用,或者它是否仍指向 500.php,这可能表明这是从其他配置中获取的,而不是你想象的)。
我正在尝试让我的网络服务器出现自定义错误 documents/pages 但是 其中包含 PHP
代码,而不会将它们重定向到页面之外引发了错误。
假设我们正在导航到 http://website.com/pages/1
并且它会抛出错误 500,根据 默认值 该页面将只是一个带有文本“的空白页面” Error 500 (Internal Server Error)
" 看起来像这样:
从上面可以看出,它没有重定向到抛出错误的页面。我希望此页面看起来 "a part of the website" 但其中包含 PHP 内容。
我无法通过编辑您在下面看到的页面在 cPanel 的错误页面中包含 PHP 内容:
如果我要编辑上面的错误 500 页面,下面的内容就是 http://website.com/pages/1
会重定向到 http://website.com/500.shtml
然后依次重定向到 http://website.com/500.php
我不想要它重定向,否则这意味着刷新页面本质上只会刷新500.php页面而不是刷新/pages/1
<script language="javascript">
window.location.href = "http://www.website.com/500.php"
</script>
<meta http-equiv="refresh" content="0;URL='http://website.com/500.php'" />
会存在完全相同的问题,只是它不是 2 个链重定向,而是使用 .htaccess 文件中的以下内容只是 1 个重定向
ErrorDocument 400 http://website.com/400.php
ErrorDocument 401 http://website.com/401.php
ErrorDocument 403 http://website.com/403.php
ErrorDocument 404 http://website.com/404.php
ErrorDocument 500 http://website.com/500.php
Current Result: Redirecting to /500.php
Expected Result: Displaying 500.php in/on http://website.com/pages/1 without redirect
我怎样才能拥有包含 php 内容的自定义错误页面而不使其重定向到引发错误的页面?
有没有办法通过 root 执行此操作(我可以让我的主机做一些事情吗?如果可以的话?)
如果您在 .htaccess 文件中添加类似这样的内容,它应该只显示错误页面而无需重定向。我从来没有像你在错误页面的例子中那样使用绝对路径,所以我不确定它会导致什么样的行为
ErrorDocument 404 /404.php
正如其他人的回答,这应该有效:
ErrorDocument 500 /500.php
如果您没有指定完整的 URL,它只会显示页面(内部重定向),如果您指定了完整的 URL,那么它会向客户端发出重定向。这是 Apache 在这里描述的:http://httpd.apache.org/docs/2.4/mod/core.html#errordocument
Note that when you specify an ErrorDocument that points to a remote URL (ie. anything with a method such as http in front of it), Apache HTTP Server will send a redirect to the client to tell it where to find the document, even if the document ends up being on the same server.
以上内容对您不起作用的事实意味着以下几种情况之一:
- 你做错了,尽管你声称不是,但仍然指定了完整的 URL。
- Cpanel 正在添加完整的 URL(不确定您是如何编辑 .htaccess 文件的,或者是否是通过上面的 GUI 完成的)。
- 您的 .htaccess 文件被忽略(可能是因为您正在 GUI 中设置值,或者可能是因为 FileInfo 未 在服务器级别设置,因此无法在 . htaccess 文件(参见此处:http://httpd.apache.org/docs/2.4/custom-error.html),在这种情况下,它必须从某处的其他配置中获取完整的 URL 本身。
- 500.php 脚本正在自行进行重定向(如果保留日志并查看是否返回一页或两页,从开发人员工具中应该很明显)。
- Apache 中存在错误(我和其他人都不知道)。
- 魔法
你能检查一下那些东西吗?
可能还有助于了解您使用的 Apache 版本?
您是否也尝试过一个普通的 500.html 文件,看看它是否可以在不重定向的情况下工作?从理论上讲,500.php 文件也应该没问题,但缩小范围会很有趣(并且还会显示对 500.html 的更改是否被拾取和使用,或者它是否仍指向 500.php,这可能表明这是从其他配置中获取的,而不是你想象的)。