英文:
Does PHP header() cache the redirect and, if so, how to prevent it from doing that?
问题
I need to show my users a slightly different version of the front-page before opening date. The idea was to redirect them to home.html
in the index.php
(the main-site) by using header("location:home.html")
, and after the opening, remove that redirect so they load the real index.php
.
The problem is, if I do that, will the browser cache the redirect and take them to cached version of home.html
even after I have removed the redirect from the index.php
? If so, how do I prevent that?
The end goal is, show the user uncached brand new version of the site after opening date.
英文:
I need to show my users a slightly different version of the front-page before opening date. The idea was to redirect them to home.html
in the index.php
(the main-site) by using header("location:home.html")
, and after the opening, remove that redirect so they load the real index.php
.
The problem is, if I do that, will the browser cache the redirect and take them to cached version of home.html
even after I have removed the redirect from the index.php
? If so, how do I prevent that?
The end goal is, show the user uncached brand new version of the site after opening date.
答案1
得分: 1
有多种类型的重定向。你想要一个临时的。
header("HTTP/1.1 307 Temporary Redirect");
header("Location: https://example.com/home.php");
PHP 不会缓存重定向,但浏览器可能会。所以当使用临时重定向时,它不会。
英文:
There are multiple kinds of redirections. You want a temporary.
header("HTTP/1.1 307 Temporary Redirect");
header("Location: https://example.com/home.php");
PHP is not caching redirects, but the browser may. So when used temporary it won't.
答案2
得分: 1
当你调用
header("location:home.html")
基本上,将会重定向到 home.html 页面。header
本身不缓存任何东西,它只是一个函数。
你的浏览器可能会缓存 CSS 和 JS 文件,但它永远不会缓存 home.html 本身。home.html 从服务器发送到浏览器的时候是原封不动的。当然,你的方程中可能有服务器端的缓存,也就是说,尽管你已经更改了 home.html,它可能在开发模式下发生了变化,然而,在服务器上仍然使用旧的 home.html,在这种情况下,缓存可能会被清空,或者以某种方式,根据你的服务器缓存品牌,你可以强制加载 home.html。
总的来说,你不应该担心 header()
使用缓存。如果你在服务器端遇到缓存问题,清空缓存或强制加载页面。如果你担心浏览器中的缓存问题,那么你可以在URL中添加一些参数,比如:
https://your/path/to/this/file.css?version=3
每当有新版本发布时,始终更改此版本,以强制浏览器加载新文件而不是重用其旧版本。
英文:
When you call
header("location:home.html")
basically, there will be a redirect to the home.html page. header
itself does not cache anything, it's just a function.
Your browser may cache CSS and JS files, but it will never cache home.html itself. home.html is being sent from your server to the browser as it is. Yet, of course, you may have server-side caching in your equation, that is, it's possible that even though you have changed home.html, it may have been changed in dev mode, whereas, an older home.html may be in use on your server, in which case the cache could be emptied, or somehow, depending on your server-cache brand you may force-load home.html.
All in all, you should not be worried about caches being used by header()
, if you have a caching problem on your server-side, empty the cache, or force-load the page. If you are afraid of caching problems in your browser, then you can add some parameter to the URL, like:
https://your/path/to/this/file.css?version=3
and always change this version whenever you have a new release to force browsers to load the new file rather than reuse its older version.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论