gpt4 book ai didi

php - 404 Header 重定向查询

转载 作者:可可西里 更新时间:2023-11-01 13:45:55 26 4
gpt4 key购买 nike

在标准的 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 header ,这是几年前我们的 SEO 团队指示的,不确定是否这对不对?这对我来说似乎违反直觉,因为它几乎暗示 404 页面本身尚未找到。

我想这是 2 个问题:

  • 当图像不是时,我是否应该在重定向中发送 404 header ?找到了吗?
  • 我的 404 页面真的应该发送 404 header 吗?

编辑

实际上似乎不可能同时发送 404 和重定向,例如这会导致 Chrome 显示“糟糕!此链接似乎已损坏。”留言

header( "Location: /404.php", true, 404 );

如果你像这样分解标题,它也不会按预期工作

header("HTTP/1.1 404 Not Found");
header("Location: /404.php" );

在这种情况下,如果您查看 header ,它会发送一个 302,然后是一个 404。在这种情况下,只发送一个 header 而不进行重定向就足够了吗?也许只是按照某些人的建议发送 410?

最佳答案

404 未找到

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

Status Code Definitions

当找不到资源时,应将 404 响应发送回客户端。

Should i send a 404 header within the redirect when the image is not found?

是的。这会告诉请求动态图像的客户端未找到该资源。如果您知道资源永远不会返回,请改用 410(消失)。

Should my 404 page actually send a 404 header?

是的。参见 404 Error Pages and Redirects for SEOs

更新

header("HTTP/1.0 404 Not Found"); 应该足够了;但是,如果内容长度小于 512 字节,谷歌浏览器将无法显示自定义 404 页面。

关于php - 404 Header 重定向查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7784386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com