404 标头重定向查询
在标准 LAMP 应用程序上,我使用 .htaccess 规则将人们发送到我的 404 页面,如下所示:
ErrorDocument 404 http://www.mydomain.com/404.php
我们使用从文件系统读取文件的 php 文件提供动态图像,但我刚刚注意到,当从文件系统中删除图像时我们没有选择此应用程序,因此请求 http://www.mydomain.com/image_4.jg(例如)使用 mod_rewrite 调用 image.php 不会将用户重定向到 404 页面,因为动态图像文件会永远存在。在这种情况下,我知道我应该使用 404,但我不确定在哪里。
显然我知道当图像被删除时我需要手动插入一个标头重定向到 404.php 页面,但我实际上应该用这个重定向发送一个 404 标头吗?看看代码,我们的 404.php 页面实际上已经发送了一个带有 ("HTTP/1.1 404 Not Found");
的 404 标头,这是我们的 SEO 团队几年前指示的,不确定是否这是正确还是错误?这对我来说似乎违反直觉,因为它几乎意味着 404 页面本身尚未找到。
我想这是两个问题:
- 当图像不是时,我应该在重定向中发送 404 标头吗? 成立?
- 我的 404 页面真的应该发送 404 标头吗?
编辑
实际上似乎不可能同时发送 404 和重定向,例如这会导致 Chrome 显示“哎呀!此链接似乎已损坏”。 如果您像这样分解标头,
header( "Location: /404.php", true, 404 );
它也不会按预期工作
header("HTTP/1.1 404 Not Found");
header("Location: /404.php" );
在这种情况下,如果您查看标头,它会发送一个 302,后跟一个 404。在这种情况下,可能只发送一个没有 404 的标头就足够了重定向?也许只是按照某些人的建议发送 410 ?
On a standard LAMP application i am sending people to my 404 page using a .htaccess rule like so:
ErrorDocument 404 http://www.mydomain.com/404.php
We serve dynamic images using a php file which reads files from the filesystem, i've just noticed though that when an image is deleted from the app that we're not picking this up so a request for http://www.mydomain.com/image_4.jg (for example) which calls image.php using mod_rewrite doesn't redirect the user to the 404 page as the dynamic image file will always exist. In this case i know i should be using a 404 but i'm not sure where.
Obviously i know i need to manually insert a header redirect to the 404.php page when the image has been deleted but should i actually send a 404 header with this redirect?. Looking at the code our 404.php page actually sends a 404 header already with ("HTTP/1.1 404 Not Found");
which is what our SEO team had instructed a few years back, not sure if this is correct or not?. It seems counter-intuitive to me as it would almost imply that the 404 page itself has not been found.
I guess this is 2 questions then:
- Should i send a 404 header within the redirect when the image is not
found? - Should my 404 page actually send a 404 header?
EDIT
It doesn't actually seem possible to send a 404 and redirect at the same time, for example this causes Chrome to show a "Oops! This link appears to be broken." message
header( "Location: /404.php", true, 404 );
If you break up the headers like this it also doesn't work as intended
header("HTTP/1.1 404 Not Found");
header("Location: /404.php" );
In this case, if you look at the headers it sends a 302 followed by a 404. Is it enough in this case to maybe just send a header without a redirect? maybe just send a 410 as recommended by some?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
404 未找到
— 状态代码定义
在以下情况下应将 404 响应发送回客户端:未找到资源。
是的。这告诉请求动态图像的客户端找不到资源。如果您知道资源永远不会返回,请改用 410(消失)。
是的。请参阅SEO 的 404 错误页面和重定向
更新
header("HTTP/1.0 404 Not Found");
应该足够了;但是,如果内容长度小于 512 字节,Google Chrome 将无法显示自定义 404 页面。404 Not Found
— Status Code Definitions
A 404 response should be sent back to the client when a resource is not found.
Yes. This tells the client requesting the dynamic image that the resource wasn't found. If you know the resource will never return, use 410 (Gone) instead.
Yes. See 404 Error Pages and Redirects for SEOs
Update
header("HTTP/1.0 404 Not Found");
should be sufficient; however, Google Chrome fails to display a custom 404 page if the content is less than 512 bytes in length.简单地说:是的,对于这两个问题。
从 image.php 发送的 404 标头不会将您重定向到 .htaccess 中指定的 404 错误文档,因此只要找不到图像,您就必须手动重定向到 404.php。首选方式是 410(消失),特别是因为您知道所请求的资源已被删除并且不会返回,因此搜索引擎可以删除该链接。
至于第二个是:它应该,尽管 Apache 已经为你做到了。
Simply said: yes, to both questions.
A 404-header sent from image.php won't redirect you to the 404 errordocument specified in .htaccess, so you'd have to manually redirect to 404.php whenever an image is not found. The preferred way is a 410 (Gone), especially since you know that the requested resource is removed and won't come back, so search engines can remove the link.
As for the second yes: it should, though Apache will already do that for you.
仅当 Web 服务器识别出请求无法映射到现有文件时,才会提供使用
ErrorDocument
指令定义的错误文档。但当您使用 mod_rewrite 将它们映射到现有的 image.php 时,它们显然会被发现。所以从Web服务器的角度来看,最初的请求可以得到满足。此时,如果无法满足特定请求并导致稍后出现 404,这并不重要。在这种情况下,image.php 需要以正确的错误响应进行响应。
您只能发送一个响应状态代码。因此,您可以发送错误响应 (4xx) 或重定向响应 (3xx)。在您的情况下,使用绝对 URL 作为文档 URL 将产生重定向状态代码(即 302)而不是源 404。
是的当然。这就是状态代码的用途。
并且还提供有关该错误文档的一些有用信息。没有什么比无意义的错误文档更糟糕的了。
The error documents defined using the
ErrorDocument
directive are only served when the web server recognizes that the request cannot be mapped onto an existing file. But as you’re using mod_rewrite to map them onto an existing image.php, they are obviously found. So from the web server perspective, the initial request can be fulfilled.At this point, it doesn’t matter if the particular request cannot be fulfilled and results in a 404 later. In that case the image.php needs to respond with a proper error response.
You can only send one response status code. So you can either send an error response (4xx) or a redirection response (3xx). In your case, having an absolute URL as document URL will yield a redirect status code (i.e. 302) instead of the origin 404.
Yes, of course. That’s what the status codes are meant for.
And do also provide some helpful information on that error documents. There is nothing worse than meaningless error documents.
您可能想要包含 404 页面,仅发送标头会给您一个空白页
You may want to include the 404 page, sending only the header will give you a blank page