2

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.

Damainman
  • 515
  • 9
  • 21
  • 1
    try to put an `exit();` after your last `header();` call, as told in this post: http://stackoverflow.com/questions/7324645/php-header-redirect-301-what-are-the-implications – lucasvscn Nov 14 '13 at 17:04
  • I am not sure what that would do. There is no other code on the page except what I posted above. I was under the impression that exit() was used if the page had additional code or if you were doing a functional redirect based on certain variables that needed to not be ran. I might be wrong though. – Damainman Nov 14 '13 at 17:11
  • I don't know what's the exaplanation to that. but, I think it worth the try ;) – lucasvscn Nov 14 '13 at 17:15

2 Answers2

3

Try this:

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

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

  • I am accepting your answer. The browser does indeed ignore it, at least for the latest version of chrome and firefox but clicking refresh properly allows the redirect for some reason. Before the cache needed to be refreshed so not really sure why. – Damainman Nov 26 '13 at 07:40
  • Put it before your code, you can also just add in the "exit();" after your header redirect if you wish. It won't hurt anything, and I tend to do that when I KNOW I wanted to redirect at that point of the code, and don't want anything else to happen below that point. I realize you aren't doing anything else on the page, but it's not a bad habit to get into. – Edward Charkow Nov 26 '13 at 17:34
0

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

endyourif
  • 2,186
  • 19
  • 33