如何为我的网站创建自定义“找不到文件(404、500)[http错误代码]”

时间:2018-12-28 09:02:18

标签: html web http-status-code-404 http-error file-not-found

我已经看到一些网站有自己的未找到带有徽标的“文件未找到”页面。

我希望在我的网站上有一个这样的页面。

请告诉我如何创建此类页面,以向用户显示是否找不到文件。

3 个答案:

答案 0 :(得分:0)

如果您使用的是Apache服务器 在您的项目文件夹中 认为您的项目在

"projects": {
  {projectName}: {
    "architect": {
      "build": {
        "configurations": {
          "production": {
            "scripts": [
              "./node_modules/jquery/dist/jquery.min.js",
              "./node_modules/slick-carousel/slick/slick.min.js",
              "./src/assets/scripts/Myscript.js"
            ]
          }
        }
      }
    }
  }
}

如果您没有/mySampleProject 文件,请在.htaccess中创建文件。

只需将此行添加到您的.htaccess中

/mySampleProject

答案 1 :(得分:0)

首先,您需要一个HTML或PHP文件来显示404发生的时间。让我们做一个简单的HTML文件,如下所示:

<h2> 404 Not Found! </h2>
<pre> Seems like the resource you're looking for is not found </pre>

我们将此文件称为404.html。

现在,您必须配置Web服务器,将未找到的请求重定向到新的HTML文档。

如果您使用的是Apache:

您可以用不同的方法来完成此操作,最常见和推荐的方法是使用.htaccess文件,该文件允许您覆盖虚拟主机的Apache配置。但是,为了能够在虚拟主机中使用.htaccess文件,您需要在Apache配置中添加allowOverride all

apache配置示例:

<Directory /path/to/your/project>
    Options FollowSymLinks
    AllowOverride All # This is the needed option
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

因此,现在您可以在项目中放入.htaccess文件,因此只需将其创建并放入其中即可:

ErrorDocument 404 /path/to/404.html/document

如果您使用的是Nginx:

您必须在nginx配置中添加错误页面。请按照本指南进行操作:

https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-to-use-custom-error-pages-on-ubuntu-14-04

答案 2 :(得分:-1)

我将建议您使用要构建此应用程序的任何编程语言的框架。他们具有针对此类要求的内置自定义安排,例如

if( file_not_found逻辑){        显示404_页    }