views:

290

answers:

1

I want to redirect this the request for "homebox/1" to "homebox/1/[uid]" if a logged in user tries to access it. I do not care about annonymous users at this point yet.

This is the code I put into the top page-homebox.tpl.php:

if(!is_numeric(arg(2))){
global $user;
if($user->uid){
 if(count($_GET) > 1){
  $get = array();
  foreach($_GET as $k=>$v){
   if($k != 'q')
    $get[] = $k.'='.$v;
  }
  $get2 = '?'.implode('&',$get);
 }
 header("HTTP/1.1 301 Moved Permanently");
 header('location:/homebox/1/'.$user->uid.$get2);
}else{
 //redirect to error page
}
}

However, do the *.tpl.php files get processed late in the request? In that case I am doing it rather inefficiently.

I know about the path redirect module(http://drupal.org/project/path%5Fredirect). I don't want to use that because I'd like to learn whether there are any Drupal API for redirecting and/or methods to catch and redirect the request right at the beginning of the processing chain.

Thanks Arul

+4  A: 

You may have had to use hook_menu_alter() but it looks like the homebox_menu() function dosn't define an entry for homebox/

So you would just write your own hook_menu implementation whih matches homebox/ and define a callback and put your logic in there.

You will probably want to user drupal_goto() to do the redirect itself.

Well done for looking to remove such logic from the template.

Jeremy French