404页面必须以404 Not Found开头并存放为根目录下的404.html;需通过服务器配置(如Apache的ErrorDocument或Nginx的error_page)返回真实HTTP 404状态码,且HTML中必须包含语义化标签,否则将被搜索引擎和屏幕阅读器忽略。

必须用 <h1>404 Not Found</h1></strong> 开头,否则搜索引擎和屏幕阅读器无法识别这是真正的 404 页面——不是“看起来像”,而是“被当作”错误页处理。</p>
<H3>HTML 文件名和存放位置必须匹配服务器配置</H3>
<p>文件名建议统一为 <code>404.html
,放在网站根目录(如 /var/www/html/ 或你托管平台的 public 根路径)。如果改用 404.php,就得确保服务器能执行 PHP,并在开头加 http_response_code(404);。常见翻车点:
- 本地双击打开
404.html—— 浏览器返回file://协议,状态码永远是 200,根本测不出效果 - 文件放错目录(比如丢进
/pages/),但服务器配置写的是ErrorDocument 404 /404.html,结果 404 请求触发默认页面 - 用了
404.htm却在 Apache 的.htaccess里写ErrorDocument 404 /404.html,后缀不一致直接失效
语义结构不能省略 <main></main> 和标题层级
搜索引擎和 Lighthouse 工具会校验语义完整性。<main></main> 必须是 的直接子元素,且只能有一个;所有用户可见内容(包括错误码、提示文案、返回链接)必须严格落在它里面。错误写法示例:
<body>
<header><h1>我的网站</h1></header>
<main>
<h1>404 Not Found</h1>
<p>页面不存在</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/cb6835dc7db1" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">前端免费学习笔记(深入)</a>”;</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/xiazai/skill13305" title="AgentGen — HTML to PDF & Image"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20cf0281539.jpg" alt="AgentGen — HTML to PDF & Image" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/skill13305" title="AgentGen — HTML to PDF & Image">AgentGen — HTML to PDF & Image</a>
<p>将 HTML 转换为 PDF 和图片。无需注册,免费版开箱即用(每分钟 1 次请求,含水印)。如需更高频次及去水印……</p>
</div>
<a href="/xiazai/skill13305" title="AgentGen — HTML to PDF & Image" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div>
</main>
<footer><p>©2026</p></footer>
</body>
上面是对的;但下面这几种都会出问题:
-
<main></main>套在<header></header>里 → 屏幕阅读器跳过全部内容 - 漏写
<main></main>,只用<div class="container"> → Lighthouse 报 “<code>mainelement not found” - 写了两个
<main></main>→ 第二个被浏览器完全忽略 - 静态场景:靠 Web 服务器配置(Apache 用
ErrorDocument 404 /404.html,Nginx 用error_page 404 /404.html;) - 动态场景:PHP 脚本首行输出
http_response_code(404);,再include '404.php';
HTTP 状态码必须由服务器发出,HTML 自身无法声明
<meta http-equiv="Status" content="404 Not Found"> 这类写法无效,浏览器不认,搜索引擎更不认。真正生效的方式只有两种:
验证是否生效的唯一方式:用 curl 或浏览器开发者工具 Network 面板看响应头,确认出现 HTTP/1.1 404 Not Found,且 Content-Type 是 text/html。
最容易被忽略的一点:即使 HTML 结构、样式、文案都完美,只要服务器没正确返回 404 状态码,这个页面就只是个普通网页,对 SEO 和无障碍访问毫无意义。


















