4

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.

1
  • try this drupal_goto('<front>');
    – Rupesh
    Commented May 6, 2014 at 1:31

2 Answers 2

3

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:

  1. Substitute original callback with yours
  2. Perform any logic needed
  3. 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('<front>')
7
  • I dont attention this note, yes , your answer is better
    – Yuseferi
    Commented May 6, 2014 at 7:53
  • @zhilevan I had an opportunity to fail at this issue already. On a busy site. I just don't want others to have the problems I gave myself then :D
    – Mołot
    Commented May 6, 2014 at 7:54
  • You are awesome molot ;)
    – Yuseferi
    Commented May 6, 2014 at 12:06
  • Thanks for the answer, as it does make sense to why different page load functionality exists in regard to login status. Fortunately, this was a URL that had not gone public as of yet, so the damage is minimal.
    – Jason
    Commented May 6, 2014 at 12:49
  • @Jason actually there are modules that cache some pages for logged-in users, too. When you use them, and have more "levels" of caching than default one, things may really get "fun". Been there, done that, got a massive server down issue ;)
    – Mołot
    Commented May 6, 2014 at 12:53
0

If you work drupal try to use drupal API, Drupal has simple api to redirect Drupal_goto

For redirecting to another url just need drupal_goto('url');

1
  • 1
    And it still may fail in tpl files, you know.
    – Mołot
    Commented May 6, 2014 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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