You are here

function path_redirect_check in Path redirect 5

If a redirect is found for the current path, perform the redirect.

1 call to path_redirect_check()
path_redirect_menu in ./path_redirect.module
Implementation of hook_menu

File

./path_redirect.module, line 26

Code

function path_redirect_check() {

  // Extract the Drupal path and query string from the request URI.
  $path = drupal_substr(urldecode(request_uri()), drupal_strlen($GLOBALS['base_path']));
  if (preg_match('/^\\?q=/', $path)) {
    $path = preg_replace(array(
      '/^\\?q=/',
      '/&/',
    ), array(
      '',
      '?',
    ), $path, 1);
  }

  // Remove trailing slash from path.
  $path = preg_replace('/\\/\\?|\\/$/', '', $path);
  $r = db_fetch_object(db_query("SELECT rid, redirect, query, fragment, type FROM {path_redirect} WHERE path = '%s' OR path = '%s'", $path, urlencode(utf8_encode($path))));
  if (!$r) {

    // If there is no match against path and query string, check just the path
    $path = preg_replace('/\\?.*/', '', $path);
    $r = db_fetch_object(db_query("SELECT rid, redirect, query, fragment, type FROM {path_redirect} WHERE path = '%s' OR path = '%s'", $path, urlencode(utf8_encode($path))));
  }
  if ($r) {
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    $r->query = strlen($r->query) ? $r->query : NULL;
    $r->fragment = strlen($r->fragment) ? $r->fragment : NULL;
    $redirect = url($r->redirect, $r->query, $r->fragment, TRUE);
    if (url($r->redirect) == url($path)) {

      // Prevent infinite loop redirection.
      watchdog('path_redirect', t('Redirect to <code>%redirect</code> is causing an infinite loop; redirect cancelled.', array(
        '%redirect' => $r->redirect,
      )), WATCHDOG_WARNING, l(t('edit'), 'admin/build/path-redirect/edit/' . $r->rid));
    }
    elseif (variable_get('path_redirect_allow_bypass', 0) && isset($_GET['redirect']) && $_GET['redirect'] === 'no') {

      // If the user has requested not to be redirected, show a message.
      drupal_set_message(t('This page has been redirected to <a href="@redirect">@redirect</a>.', array(
        '@redirect' => $redirect,
      )));
    }
    elseif (variable_get('path_redirect_redirect_warning', 0)) {

      // Show a message and automatically redirect after 5 seconds.
      drupal_set_message(t('This page has been moved to <a href="@redirect">@redirect</a> and will redirect in 5 seconds. You may want to update your bookmarks.', array(
        '@redirect' => $redirect,
      )), 'error');
      drupal_set_html_head("<meta http-equiv=\"refresh\" content=\"5;url={$redirect}\" />");
    }
    else {

      // Perform the redirect directly.
      unset($_REQUEST['destination']);
      drupal_goto($r->redirect, $r->query, $r->fragment, $r->type);
    }
  }
}