Daniele Cruciani
Programmatore Developer PHP/MySQL Freelance
mobile: +39 3489215204

icq skype msn linkedin fb t ff youtube picasa google
seconds to the end of World
End of World 21 December, 2012 11:11:00
Hire me before that day! | don't bother

Fix SEO problem in drupal (give 301 to node/xx request)

this was a problem with www.tecnomagazine.it :

http://www.tecnomagazine.it/recensioni/casio-exilim_ex-z60/442

could be accessed from /node/442

Solution is to redirect all /node/[0-9]* url to the aliased one
... and is very simple in code:

includes/path.inc :

function drupal_init_path() {
  if (!empty($_GET['q'])) {
    // add this conditional
    if(preg_match('/node\/[0-9]*$/',$_GET['q'])) {
      include "includes/common.inc";
      $newq = trim(url($_GET['q']),'/');
      if($newq!=$_GET['q']) {
	drupal_goto(trim($_GET['q'],'/'),NULL,NULL,301);
      }
    }
    $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
  }
  else {
    $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  }
}

drupal_init_path() is called in bootstrap as sixth pass and find the internal path (like /node/[0-9]*)
Here I check if the request contains already a normal path, check if there is
aliased path, if so goto that path and give 301

update (16 August, 2010)
is better to use this (add conditional and goto) in custom_url_rewrite_inbound() function:
http://api.drupal.org/api/function/custom_url_rewrite_inbound/6
and do not touch drupal main code

see also
http://drupal.org/project/globalredirect