From 6b4dc697287f544b5c3d0a0ff2fe28a03de8e268 Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Thu, 14 Jul 2011 10:34:50 -0400
Subject: [PATCH] Issue #905914: Enable global redirects.

---
 redirect.module |   51 +++++++++++++++++++++++++++++++--------------------
 1 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/redirect.module b/redirect.module
index 6e8d9b24200be8e70b0f3741c7ac58c88be17157..09355f5764d10ba8d624bda2b2be63c17e4a6355 100644
--- a/redirect.module
+++ b/redirect.module
@@ -206,19 +206,30 @@ function redirect_menu() {
  * Implements hook_url_inbound_alter().
  */
 function redirect_url_inbound_alter(&$path, $original_path, $path_language) {
-  // Redirect to canonical URLs.
-  if ($path && variable_get('redirect_canonical', 1)) {
-    $alias = drupal_get_path_alias($path, $path_language);
-    if ($alias != $path && $alias != $original_path) {
-      //return redirect_redirect(array('redirect' => $alias, 'type' => 'global'));
-    }
+  // Check for empty path or disabled canonical redirects.
+  if (empty($path) || !variable_get('redirect_canonical', 1)) {
+    return;
+  }
+  // Do not redirect from cron.php or anywhere else but index.php.
+  $script_path = $_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME'];
+  if ($script_path !== $GLOBALS['base_path'] . 'index.php') {
+    return;
+  }
+  // Do not redirect */search/redirect or */search/path pages.
+  if (arg(2) === 'search' && (arg(3) === 'redirect' || arg(3) === 'path')) {
+    return;  
+  }	     
+  $alias = drupal_get_path_alias($path, $path_language);
 
-    // Redirect from default entity paths to the proper entity path.
-    if ($path_entity = redirect_load_entity_from_path($path)) {
-      $uri = entity_uri($path_entity['entity_type'], $path_entity['entity']);
-      if ($path != $uri['path']) {
-        //return redirect_redirect(array('redirect' => $uri['path'], 'redirect_options' => $uri['options'], 'type' => 'global'));
-      }
+  if ($alias != $path && $alias != $original_path) {
+    return redirect_redirect((object)array('redirect' => $alias, 'type' => 'global'));
+  }
+
+  // Redirect from default entity paths to the proper entity path.
+  if ($path_entity = redirect_load_entity_from_path($path)) {
+    $uri = entity_uri($path_entity['entity_type'], $path_entity['entity']);
+    if ($path != $uri['path']) {
+      return redirect_redirect((object)array('redirect' => $uri['path'], 'redirect_options' => $uri['options'], 'type' => 'global'));
     }
   }
 }
@@ -260,20 +271,20 @@ function redirect_init() {
 
   // Redirect from non-clean URLs to clean URLs.
   if (variable_get('redirect_global_clean', 1) && variable_get('clean_url', 0) && strpos($request_uri, '?q=') !== FALSE) {
-    //$redirect_global = TRUE;
-    //$request_uri = str_replace('?q=', '', $request_uri);
+    $redirect_global = TRUE;
+    $request_uri = str_replace('?q=', '', $request_uri);
   }
 
   if (strpos($request_uri, 'index.php') !== FALSE) {
-    //$redirect_global = TRUE;
-    //$request_uri = str_replace('index.php', '', $request_uri);
+    $redirect_global = TRUE;
+    $request_uri = str_replace('index.php', '', $request_uri);
   }
 
-  //$request_uri = ltrim($request_uri, '/');
-  //$parsed = parse_url($request_uri);
+  $request_uri = ltrim($request_uri, '/');
+  $parsed = parse_url($request_uri);
 
   if ($redirect_global && $request_uri != $original_uri) {
-    redirect_redirect(array(/*'redirect' => $request_uri,*/ 'type' => 'global'));
+    redirect_redirect(array('redirect' => $request_uri, 'type' => 'global'));
   }
 }
 
@@ -636,7 +647,7 @@ function redirect_validate($redirect, $form, &$form_state) {
   }
 }
 
-function redirect_object_prepare($redirect, $defaults = array()) {
+function redirect_object_prepare(stdClass $redirect, $defaults = array()) {
   $defaults += array(
     'rid' => NULL,
     'type' => 'redirect',
-- 
1.7.4.1

