PHP header redirect 301 - what are the implications? PHP header redirect 301 - what are the implications? php php

PHP header redirect 301 - what are the implications?


The effect of the 301 would be that the search engines will index /option-a instead of /option-x. Which is probably a good thing since /option-x is not reachable for the search index and thus could have a positive effect on the index. Only if you use this wisely ;-)

After the redirect put exit(); to stop the rest of the script to execute

header("HTTP/1.1 301 Moved Permanently"); header("Location: /option-a"); exit();


This is better:

<?php//* Permanently redirect pageheader("Location: new_page.php",TRUE,301);?>

Just one call including code 301. Also notice the relative path to the file in the same directory (not "/dir/dir/new_page.php", etc.), which all modern browsers seem to support.

I think this is valid since PHP 5.1.2, possibly earlier.


Just a tip: using http_response_code is much easier to remember than writing the full header:

http_response_code(301);header('Location: /option-a'); exit;