71

I have example.com. If the user is logged in, it should load automatically example.com/option-X where X is a predefined choice of the user.

So, I do this at the top of index.php:

header("Location: /option-X");

But, if the user is not logged in, I just choose automatically the first option like this:

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

So, I have two questions regarding the implications of doing so:

  1. Since the search engines crawlers won't be logged in, they will always get example.com/option-a - does it affect them that it has a 301 header?
  2. What could be the server cpu load of doing those redirects? I don't know how to make a test out of it. The current site (which has no redirects) has about 100k daily visits.
3
  • 2
    The cpu overhead depends on what your script is doing BEFORE the redirect header goes out. if you're calculating pi to 5 million places, then doing a redirect is going to double the workload, as the browser is going to make a whole new http request for the new address. Otherwise, the cpu hit should be minimal and all it's cost you is a bit of extra bandwidth.
    – Marc B
    Commented Sep 6, 2011 at 18:52
  • Thanks Marc! All it does before the redirect is to check for a $_SESSION variable.
    – Andres SK
    Commented Sep 6, 2011 at 18:53
  • Syntax header(header, replace, http_response_code)
    – mehmet
    Commented Feb 28, 2022 at 12:21

5 Answers 5

145

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();
6
  • im assuming that using ob_start('ob_gzhandler'); before the header call will have no positive effect neither, am i right?
    – Andres SK
    Commented Sep 6, 2011 at 21:16
  • Yes, indeed. To the contrary: more work for the server for no benefits.
    – pixeline
    Commented Apr 23, 2014 at 10:29
  • This is truly a great solution and I thank you for sharing it with us. I normally would have just used the Location header to redirect to my primary domain but the fact that you include a header for search engines and, equally important, added the exit() function makes perfect and logical sense to me. This is great, thanks once again. Commented Jan 3, 2018 at 23:56
  • 2
    i think shorting the code this way is more cleaner : Exit( header("Location: /option-a", true, 301) );
    – The Doctor
    Commented Jun 6, 2020 at 15:46
  • 1
    What would be the effect of this header on http2 protocol ? Commented May 1, 2021 at 1:31
66

This is better:

<?php
//* Permanently redirect page
header("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.

1
  • 2
    Syntax header(header, replace, http_response_code)
    – mehmet
    Commented Feb 28, 2022 at 12:22
23

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;
4

Make sure you die() after your redirection, and make sure you do your redirect AS SOON AS POSSIBLE while your script executes. It makes sure that no more database queries (if some) are not wasted for nothing. That's the one tip I can give you

For search engines, 301 is the best response code

3

Search engines like 301 redirects better than a 404 or some other type of client side redirect, no worries there.

CPU usage will be minimal, if you want to save even more cycles you could try and handle the redirect in apache using htaccess, then php won't even have to get involved. If you want to load test a server, you can use ab which comes with apache, or httperf if you are looking for a more robust testing tool.

Not the answer you're looking for? Browse other questions tagged or ask your own question.