This code performs 301 redirects based on the taxonomy terms associated with the node, as 301 redirects can adversely effect page load time, how could this be best optimized.
<?php
/**
* Implementation of hook_init().
*calling this hook causes the code below to execute before the page is loaded
*/
function region_filter_init() {}
//hook "nodeapi" is called when we are doing something to the node, in this "case" we are "viewing" the node.
function region_filter_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'view':
if( (arg(1) == 0)){ } //do not execute on homepage. if there are no aruments after the base url in arument one is thedelimagazine.com/argument1, if this is 0 then do nothing.
else {
$region = getRegion(); //get current region from deli.mmodule
$national = 'national'; //store string 'national' in variable
$count = count($node->taxonomy);//nstores number of key/value pairs in array, if two terms are selected with the node/post then count == 2
$terms = array();//initialize terms to array
foreach($node->taxonomy as $term) {
$terms[$term->name] = $term->name; //prints array of individual terms
}
/*case 1 new england*/
//if there are less than 9 terms and more than 2 and new england is in the array redirect to new england
if( $count < 9 && $count > 2 && in_array('newengland', $terms) && $region != 'newengland')
{
$link = 'http://newengland.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');
drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);
}
/*END case 1 new england*/
/*case 2*/
if($count == 2 && in_array('national', $terms) && is_numeric(arg(1))) { //if there are 2 terms in taxonomy && national is one of them & the first argument in url is a number such as thedelimagazine.com/12345 (before alias redirect)
unset($terms['national']); //remove national from array
$value = array_shift($terms) ;//set value to the other term left. in array
if($value == 'los angeles') {//fix for los angelas, because in deli module taxonomy term and subdomain are different only for los angeles
$value = 'la';
}
if($region != $value) { //if region does not already = term (would cause redirect loop)
$link = 'http://' . $value . '.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//go to term left in $value
drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301);//drupal redirect function
}
}
/* ++ END case 2++*/
/* ++case 3 national+++*/
else {
if($count > 10 && in_array('national', $terms) && $region != 'national' ) { //if there are moer than 10 terms and national is in the array $terms, and the current region does not already =national
$link = 'http://national.thedelimagazine.com/' . drupal_get_path_alias("node/$node->nid", $path_language = '');//then set $link to national.thedelimagazine.com. drupl get path alias gets the path to the node and node id and converts it to clean readable urls that i implemented last year.
drupal_goto($link, $query = NULL, $fragment = NULL, $http_response_code = 301); //go to the $link
}
/* ++ END case 3 national++*/
}
}
break;
}
}