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

I have domain.com. If the user is logged in, it should load automatically domain.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 domain.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.

This question is related to php redirect header http-status-code-301

The answer is


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.


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.


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;

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


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to redirect

React-Router External link Laravel 5.4 redirection to custom url after login How to redirect to another page in node.js How to redirect to an external URL in Angular2? How to redirect to a route in laravel 5 by using href tag if I'm not using blade or any template? Use .htaccess to redirect HTTP to HTTPs How to redirect back to form with input - Laravel 5 Using $window or $location to Redirect in AngularJS yii2 redirect in controller action does not work? Python Requests library redirect new url No 'Access-Control-Allow-Origin' header in Angular 2 app How to add header row to a pandas DataFrame How can I make sticky headers in RecyclerView? (Without external lib) Adding header to all request with Retrofit 2 Python Pandas Replacing Header with Top Row Request header field Access-Control-Allow-Headers is not allowed by Access-Control-Allow-Headers Pythonically add header to a csv file fatal error C1010 - "stdafx.h" in Visual Studio how can this be corrected? correct PHP headers for pdf file download How to fix a header on scroll

Examples related to http-status-code-301

How long do browsers cache HTTP 301s? PHP header redirect 301 - what are the implications? Redirecting 404 error with .htaccess via 301 for SEO etc .htaccess 301 redirect of single page HTTP redirect: 301 (permanent) vs. 302 (temporary) How do I make a redirect in PHP?