PHP header 301 redirect not working due to browser cache

倖福魔咒の 提交于 2020-01-05 04:01:37

问题


My code is the following:

<?php 
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: http://newsite.com/"); 
?>

The issue I am having is several users have already reported the page not redirecting them and instead showing them the content on the old page, but later in the day it showed the correct redirect.

I know it has to be related to the caching but which is the most efficient and standardized way to bypass the cache for the above redirect? Would I use php or javascript?

Thank you.


回答1:


Try this:

header('Cache-Control: no-cache');
header('Pragma: no-cache');

The browser may ignore your request, but this should do the trick.




回答2:


The correct answer is what lucasvscn said with adding exit(); after the header tags. Cache control mention above actually caused an error for me.



来源:https://stackoverflow.com/questions/19983660/php-header-301-redirect-not-working-due-to-browser-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!