找回密码
 立即注册
首页 资源区 问答 网站配置404页面教程

网站配置404页面教程

新程序 昨天 17:31
配置404页面需要根据你的网站服务器类型进行设置。以下是常见服务器(Apache、Nginx)和静态网站托管平台的配置方法,以及如何结合之前提供的404页面代码。
1. 使用之前提供的404页面
确保你已经将之前的404页面代码保存为 404.html 文件,并上传到你的网站根目录或指定目录。
2. 服务器配置方法Apache 服务器
在 Apache 服务器上配置404页面,可以通过修改 .htaccess 文件或 Apache 配置文件。
  • 通过 .htaccess 文件
    • 在网站根目录下创建或编辑 .htaccess 文件。
    • 添加以下代码,指向你的404页面:apache
      [color=var(--fg-primary)][size=0.9em]
      1. <span style="color: rgb(123, 48, 208);">ErrorDocument</span><span style="color: rgb(23, 71, 129);"> 404</span> /<span style="color: rgb(23, 71, 129);">404</span>.html
      复制代码
    • 确保 404.html 文件位于根目录。如果在子目录(如 /errors/404.html),则修改为:apache

      [color=var(--fg-primary)][size=0.9em]
      1. <span style="color: rgb(123, 48, 208);">ErrorDocument</span><span style="color: rgb(23, 71, 129);"> 404</span> /errors/<span style="color: rgb(23, 71, 129);">404</span>.html
      复制代码

  • 通过 Apache 配置文件
    • 编辑 Apache 的配置文件(通常是 httpd.conf 或站点配置文件,如 /etc/apache2/sites-available/000-default.conf)。
    • 添加以下行:apache

      1. <span style="color: rgb(123, 48, 208);">ErrorDocument</span><span style="color: rgb(23, 71, 129);"> 404</span> /<span style="color: rgb(23, 71, 129);">404</span>.html
      复制代码

    • 保存后重启 Apache:bash



      [color=var(--fg-primary)][size=0.9em]
      1. <span style="color: rgb(126, 178, 51);">sudo</span><span style="color: rgb(164, 65, 133);"> systemctl</span><span style="color: rgb(164, 65, 133);"> restart</span><span style="color: rgb(164, 65, 133);"> apache2</span>
      复制代码


Nginx 服务器
在 Nginx 中配置404页面,需要修改 Nginx 配置文件。
  • 编辑 Nginx 配置文件
    • 找到你的 Nginx 配置文件(通常是 /etc/nginx/nginx.conf 或 /etc/nginx/sites-available/default)。
    • 在 server 块中添加以下代码:nginx



      [color=var(--fg-primary)][size=0.9em]
      1. <span style="color: rgb(123, 48, 208);">error_page </span><span style="color: rgb(23, 71, 129);">404</span> /404.html;
      2. <span style="color: rgb(9, 145, 182);">location</span><span style="color: rgb(123, 48, 208);"> =</span><span style="color: rgb(164, 65, 133);"> /404.html </span>{
      3. <span style="color: rgb(123, 48, 208);">    root </span>/var/www/html; <span style="color: rgb(53, 123, 66); font-style: italic;"># 替换为你的网站根目录</span>
      4. <span style="color: rgb(123, 48, 208);">    internal</span>;
      5. }
      复制代码


相关推荐

您需要登录后才可以回帖 登录 | 立即注册