发送 404 找不到文件标头,然后提供备用文件以供下载


send 404 File not found header, and then provide alternative file for download

我有一个文件名中包含版本号的文件,几乎每个月都会更新。版本号对于需要它才能正常工作的其他软件是必需的。
为了使具有旧版本号的已发布链接也可行,为了方便用户,我编写了一个php脚本,该脚本使用301标头执行重定向,并提供具有当前版本号的最新文件以供下载。

现在我在日志文件中发现带有旧版本号的旧链接保存在搜索引擎的索引中,并且它们越来越多地用于旧文件(url),即使使用 301 重定向,他们也不会更新存储的链接并一遍又一遍地请求旧文件。
阅读了一段时间后,我发现将它们从索引中取出的唯一方法是发送 404 或 410 标头,但另一方面,这似乎不允许我发送带有该标头的文件。


所以问题是你是否有机会让我如何发送 404 或 410 标题以从搜索引擎索引中获取旧链接,同时提供最新的文件以供下载以使旧链接仍然为用户舒适而工作?

你可以结合使用meta http-eqiv属性和JavaScript来允许来自浏览器的请求完成。此方法不适用于wgetcurl或其他此类自动化程序。

<!doctype html>
<html>
  <head>
    <title>File not found</title>
    <meta charset="utf-8"/>
    <meta http-equiv="refresh" content="5;URL=http://somedomain.tld/file_v1.txt"/>
  </head>
  <body>
    <p>The requested resource does not exist. However, an older version of the file was found and will begin downloading in <span id="countdown">5</span> seconds.</p>
    <p>Please <a href="http://somedomain.tld/file_v1.txt">click here</a> if the download does not begin or you no longer wish to wait.</p>
    <script>
    // you can get #countdown and using setInterval decrease the seconds to enhance the user experience
    // additionally, you can use `window.location.href = "http://somedomain.tld/file_v1.txt";` at 0 seconds
    // incase the user has meta redirects disabled
    </script>
  </body>
</html>