oliverwhite oliverwhite - 1 month ago 7
PHP Question

The best way to do a 301 redirect with a delay of several seconds?

What is the best way to do a 301 redirect with a delay of several seconds? I want the original page to be displayed for 5-10 seconds and then do a 301 redirect to another site. I've found a lot of solutions in PHP on google but the only one I found with a delay didn't display the original page before redirecting—only an empty screen.

Answer

You can't really do a true 301 redirect with a delay. HTTP is stateless. For a "301 redirect", which is what you want if you're trying to make google happy; the client sends a request, and the status code on the reply from the server will be 301, also as part of the reply you'll use a location header and tell where the new content is. If you don't have all that, you're not doing a 301 redirect.

With the other answer, you're doing a meta refresh on the client side, which google does not like.

This is how you do a 301, and the user doesn't even see the old page at all, and probably will have no idea they've even been redirected.

$location="http://www.yoursite.com/newpage";
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: '.$location);