简体   繁体   English

如果网页不存在,我应该返回哪些标题?

[英]If a web page doesn't exist, what headers should I return?

What should happen when a page doesn't exist on your site? 当您的网站上不存在网页时会发生什么? If I have a custom error page set up in my .htaccess, I get a 302 temporary redirect to my 404 page (where I send 404 headers). 如果我在.htaccess中设置了自定义错误页面,我会获得302临时重定向到我的404页面(我发送404标题)。 Is this how it should work? 它应该如何工作? Or should I do a 301 permanent redirect to the error page? 或者我应该将301永久重定向到错误页面? I'm using php and Apache. 我正在使用php和Apache。

If it never existed, you should send 404 Not Found . 如果它从未存在过,则应发送404 Not Found

If it used to exist, but doesn't any more, you should send 410 Gone . 如果以前存在但不再存在,则应发送410 Gone

Set up your server to send the error response directly. 设置服务器以直接发送错误响应。

You shouldn't redirect to an error page. 您不应该重定向到错误页面。 That essentially means the conversation would go: 这基本上意味着谈话将会:

  • Browser: Can I have /foo? 浏览器:我可以拥有/ foo吗?
  • Server: You can find /foo at /bar! 服务器:您可以在/ bar找到/ foo!
  • Browser: Can I have /bar then? 浏览器:我可以/吧吗?
  • Server: I can't find /bar. 服务器:我找不到/吧。

Bait and switch is never nice. 诱饵和开关永远不会好。

The non-existent page has not "temporarily moved" (301) or "permanently moved" (302) to a page about the file not existing, it doesn't exist. 不存在的页面没有“暂时移动”(301)或“永久移动”(302)到关于不存在的文件的页面,它不存在。 The correct header to send is a 404, not anything else. 要发送的正确标头是404,而不是其他任何标头。

Don't redirect anywhere, instead serve your "file not found" page content at the URL that was requested. 不要在任何地方重定向,而是在请求的URL上提供“找不到文件”页面内容。

If you got a static error page, say 404.html, that apache shows when a page is not found, and you need to be able to serve 404 pages for your dynamic pages as well, you can do that by sending the 404 header from PHP and feed the error page as well: 如果您有一个静态错误页面,比如说404.html,那么apache会在找不到页面时显示,并且您需要能够为动态页面提供404页面,您可以通过发送404标题来实现PHP并提供错误页面:

<?php
header("HTTP/1.0 404 Not Found");
readfile("404.html");
?>

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

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