如何进行非缓存301重定向? [英] How to do a non-cached 301 redirect?

查看:143
本文介绍了如何进行非缓存301重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间所有浏览器都改变了行为并开始缓存301重定向,我想知道如何进行未在php中缓存的301重定向?

A while ago all browsers changed their behaviour and started caching 301 redirects, I would like to know how to do a 301 redirect that is not cached in php?

推荐答案

301 永久重定向,因此缓存很有意义。如果您的重定向不是永久性的,请使用 307 (临时重定向), 302 (找到)或 303 (请参阅其他)。

301 is a permanent redirect, so caching makes sense. If your redirect isn't permanent, use 307 (temporary redirect), 302 (found) or 303 (see other).

见这里

详细说明这三者之间的差异:

To elaborate on the differences between these three:


  • 307 是资源移动时的通用临时重定向。例如,像 domain.com/news/latest 这样的网址可能会重定向到最新的新闻文章, domain.com/news/article -594873 。由于此临时重定向可能持续一段时间(该特定文章可能是几个小时内的最新文章),因此浏览器可能缓存重定向。要控制它们的工作程度,请使用缓存控制标头。

  • 303 不得缓存的重定向, 。例如,将新文章发布到 domain.com/news 可能会创建一个新的新闻文章,并向其提供303重定向到域。 COM /新闻/条-978523 。由于另一个 POST请求会导致创建一个完全不同的新文章,因此无法对其进行缓存。

  • 302 有点陌生,我自己从未使用过它。显然它更像是303的遗留替代品,对于不了解303的早期HTTP 1.0版客户端。

  • 307 is the generic, temporary redirect for when a resource is moved. For example, a URL like domain.com/news/latest might do a 307 redirect to the latest news article, domain.com/news/article-594873. Since this temporary redirection may persist for a while (that particular article may be the latest for several hours), browsers might cache the redirect. To control the degree to which they do, use cache control headers.
  • 303 is the redirect that must not be cached, ever. For example, POSTing a new article to domain.com/news might create a new news article, and a 303 redirect to it is provided to domain.com/news/article-978523. Since another POST request results in a completely different, new article being created, it cannot be cached.
  • 302 is a bit stranger, I've never used it myself. Apparently it's more of a legacy substitute for the 303, for earlier HTTP 1.0 version clients who do not understand the 303.

因为你问过特别是关于PHP:

Since you asked specifically about PHP:

<?php
function header_redirect_permanent($url)
    {
    header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently', true, 301);
    header('Location: ' . $url);
    }

function header_no_cache()
    {
    header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // past date to encourage expiring immediately
    }

您可以阻止座席缓存301同样,如果必须,使用上面这样的缓存控制标题:

You can stop agents from caching a 301 as well, if you must, using the above cache control headers like this:

header_no_cache();
header_redirect_permanent($url);

或只需添加

header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Location:'.$url, true, 301);
exit;

这篇关于如何进行非缓存301重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆