header("Location: /") redirect in template file only works if user is logged in. Why? I was developing a new feature for a client, and deployed it live today.  However, due to circumstances out of my control, they decided to implement an alternative solution and asked me to implement a temporary redirect.

This feature was a standalone full screen flash app that did not fit the site theme at all.  So, I implemented the page as a custom html--pagename.tpl.php file and loaded the page-specific JS and CSS.  It is a custom template for an existing content type.

Now, since the client wants a temporary redirect, I added

header("Location: /");


as the first line of PHP code in the template to redirect any access to that URL to the index page.  However, this only works if the user is logged in.  Anyone 
else can view this page and the header code seems to be ignored for some reason.

This URL loads regardless of the Published status set to Published or Unpublished.  In the morning, I will modify the .htaccess to execute a 302 redirect, but would like an explanation for this behavior for future reference.
Output of the tpl files is cached for anonymous users. But only their text output is, that's how Output Control Functions in PHP works. That's why Drupal API functions will not work as intended, either. They may do their job, all right, by serving as cache breakers, but you do not want to break cache on a busy website, as it may be a server killer.

Most stable way of doing temporary redirections is, as far as I found, an .htaccess way. mod_rewrite is already used by Drupal, so it cost you nothing in additional binaries loaded to memory, and it acts before PHP is even started. As described in this answer on SO, you can do this like that:

RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]


Put it before Drupal redirects, but in the same section.

If you need a PHP logic to decide if redirect is needed, you can do it using hook_menu_alter() instead:


Substitute original callback with yours
Perform any logic needed
Log to watchdog if you want to

Call original callback and return it's result if you are not redirecting, or
Redirect with drupal_goto('&lt;front&gt;')