简体   繁体   English

PHP 中的自定义 404 错误页面

[英]Custom 404 Error Page in PHP

I am a novice in PHP, I hope you can help me with this problem.我是 PHP 的新手,希望你能帮我解决这个问题。 How can I create a custom 404 error page in PHP without going anywhere or redirect it to other page.如何在 PHP 中创建自定义 404 错误页面而不去任何地方或将其重定向到其他页面。

Ex.前任。 mysite.com/no-where

The page no-where does not exists.页面no-where存在。 I want to display a 404 error message with design on that URL.我想在 URL 上显示带有设计的 404 错误消息。

How can I get the current URL?如何获得当前的 URL? I got an error on this.我对此有误。 Coz I want to check the header if its 404.因为我想检查 header 是否为 404。

$url = $_SERVER['REQUEST_URI'];
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200")) {
    echo 'url exists';
} else {
    echo 'url does not exist';
}

Create a .htaccess file and put the following line in it:创建一个 .htaccess 文件并将以下行放入其中:

ErrorDocument 404 /errorpageFileName.php 

That will set the page 'errorpageFileName.php to your error 404 page.这会将页面 'errorpageFileName.php 设置为您的错误 404 页面。 You can of course change the file name to your likings.您当然可以根据自己的喜好更改文件名。

You can read more in-depth about it here: Custom 404 error issues with Apache您可以在这里更深入地阅读: 自定义 404 错误问题与 Apache

It's really important to have a correct 404 error page, because:拥有正确的 404 错误页面非常重要,因为:

  • It helps your site / webapp visitors to guide for the correct place in your site它可以帮助您的网站/webapp 访问者指导您网站中的正确位置
  • Much better SEO更好的搜索引擎优化
  • It's just looks better只是看起来更好

In additional the the answers here, I would suggest you to have different messages for different error types.除了这里的答案之外,我建议您为不同的错误类型提供不同的消息。 For example, define in your .htacess file an error page for different failure types:例如,在您的.htacess文件中为不同的故障类型定义一个错误页面:

ErrorDocument 500 /500.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html

More information about custom 404 error pages .有关自定义 404 错误页面的更多信息。

If you are using apache as a web server you can create a file named .htaccess in the root directory of your website.如果您使用 apache 作为 Web 服务器,您可以在您网站的根目录中创建一个名为.htaccess的文件。 This can contain various rules, including error handling, as follows:这可以包含各种规则,包括错误处理,如下所示:

ErrorDocument 404 (Your site error 404 page URL)

For example:例如:

ErrorDocument 404 http://example.domain/custom-404-page.html

在 .htaccess 中添加这一行

ErrorDocument 404 "Page not found."

Use a 404 header and then require your 404 template / page.使用 404 标头,然后需要您的 404 模板/页面。

header('HTTP/1.0 404 Not Found');
include '404.php';
exit;

That or you can configure Apache to serve up a custom page.或者您可以配置 Apache以提供自定义页面。

If you are using apache as a web server and cpanel, just click menu error page, choose 404 error not found and edit the editor with your custom html page and save.如果您使用 apache 作为 Web 服务器和 cpanel,只需单击菜单错误页面,选择 404 错误未找到并使用您的自定义 html 页面编辑编辑器并保存。 You will get automatic file 404.shtml in your folder.您将在文件夹中获得自动文件 404.shtml。

If you are using vps openlitespeed and webadmin panel, there is error not found and fill with your custom 404 html page.如果您使用的是 vps openlitespeed 和 webadmin 面板,则找不到错误并填写您的自定义 404 html 页面。

If your panel is cyberpanel at openlitespeed, just create your custom 404 html page (ex:404.html), then edit your vHost Conf and fill with line:如果您的面板是 openlitespeed 的网络面板,只需创建您的自定义 404 html 页面(例如:404.html),然后编辑您的 vHost Conf 并填写以下行:

errorpage 404 {
  url                     /404.html
}

And save it并保存它

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

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