'. t('Here you can set up URL redirecting for this site. Any existing or non-existing path within this site can redirect to any internal or external URL.') .'
'; case 'admin/build/path-redirect/'. arg(2): case 'admin/build/path-redirect/edit/'. arg(3): return ''. t("The from path must be an internal Drupal path in the form of 'node/123', 'admin/logs', or 'taxonomy/term/123'. The to path can be either an internal Drupal path as above or a complete external URL such as http://www.example.com/. Furthermore, the to path may contain query arguments (such as 'page=2') and fragment anchors, to make it possible to redirect to 'admin/user?page=1#help'. Most redirects will not contain queries or anchors.") .'
'; } } /** * If a redirect is found for the current path, perform the redirect. */ 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%redirect
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 @redirect.', 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 @redirect and will redirect in 5 seconds. You may want to update your bookmarks.', array('@redirect' => $redirect)), 'error');
drupal_set_html_head("");
}
else {
// Perform the redirect directly.
unset($_REQUEST['destination']);
drupal_goto($r->redirect, $r->query, $r->fragment, $r->type);
}
}
}
/**
* Implementation of hook_menu
*/
function path_redirect_menu($may_cache) {
$access = user_access('administer redirects');
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/build/path-redirect',
'title' => t('URL redirects'),
'description' => t('Redirect users from one URL to another'),
'callback' => 'path_redirect_admin',
'access' => $access,
);
$items[] = array(
'path' => 'admin/build/path-redirect/list',
'title' => t('List'),
'description' => t('List all URL redirects'),
'access' => $access,
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -3,
);
$items[] = array(
'path' => 'admin/build/path-redirect/add',
'title' => t('Add redirect'),
'description' => t('Add a new URL redirect'),
'callback' => 'drupal_get_form',
'callback arguments' => array('path_redirect_edit'),
'access' => $access,
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/build/path-redirect/edit',
'title' => t('Edit'),
'description' => t('Edit an existing URL redirect'),
'callback' => 'drupal_get_form',
'callback arguments' => array('path_redirect_edit'),
'access' => $access,
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/path-redirect/delete',
'title' => t('Delete'),
'description' => t('Delete an existing URL redirect'),
'callback' => 'drupal_get_form',
'callback arguments' => array('path_redirect_delete_confirm'),
'access' => $access,
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'admin/build/path-redirect/settings',
'title' => t('Settings'),
'description' => t('Configure behavior for URL redirects'),
'callback' => 'drupal_get_form',
'callback arguments' => 'path_redirect_settings',
'access' => $access,
'type' => MENU_LOCAL_TASK,
);
}
else {
path_redirect_check();
}
return $items;
}
/**
* Implementation of hook_perm
*/
function path_redirect_perm() {
return array('administer redirects');
}
/**
* Render a list of redirects for the main admin page.
*/
function path_redirect_admin() {
$header = array(
array('data' => t('From'), 'field' => 'path', 'sort' => 'asc'),
array('data' => t('To'), 'field' => 'redirect'),
array('data' => t('Type'), 'field' => 'type'),
array('data' => t('Operations'), 'colspan' => '2'),
);
$result = pager_query('SELECT rid, path, redirect, query, fragment, type FROM {path_redirect}'. tablesort_sql($header), 50);
$rows = array();
while ($r = db_fetch_object($result)) {
$redirect = url($r->redirect, ($r->query ? $r->query : NULL), ($r->fragment? $r->fragment : NULL), TRUE);
$rows[] = array(
// @todo: Revise the following messy, confusing line.
l($r->path, preg_replace('/\?.*/', '', $r->path), array(), strstr($r->path, '?') ? preg_replace('/.*\?/', '', $r->path) : NULL),
l($redirect, $redirect),
$r->type,
l(t('edit'), 'admin/build/path-redirect/edit/'. $r->rid),
l(t('delete'), 'admin/build/path-redirect/delete/'. $r->rid),
);
}
if (empty($rows)) {
$rows[] = array(array('data' => t('No redirects have been added.'), 'colspan' => '5'));
}
$output = theme('table', $header, $rows, array('class' => 'path-redirects'));
$output .= ''. l(t('Add redirect'), 'admin/build/path-redirect/add') .'
'; $output .= theme('pager'); return $output; } /** * Callback for add and edit pages. * * @return * A form for drupal_get_form. */ function path_redirect_edit($rid = FALSE) { if ($rid) { $redirect = path_redirect_load($rid); drupal_set_title(check_plain($redirect['path'])); $output = path_redirect_edit_form($redirect); } else { $breadcrumbs = drupal_get_breadcrumb(); array_push($breadcrumbs, l(t('URL redirects'), 'admin/build/path-redirect')); drupal_set_breadcrumb($breadcrumbs); drupal_set_title(t('Add redirect')); $output = path_redirect_edit_form(); } return $output; } function path_redirect_edit_form($edit = array('path' => '', 'redirect' => '', 'query' => '', 'fragment' => '', 'type' => PATH_REDIRECT_DEFAULT_TYPE, 'rid' => NULL)) { $form['path'] = array( '#type' => 'textfield', '#title' => t('From'), '#description' => t('Enter a Drupal path or path alias to redirect. Fragment anchors #foo are not allowed.'), '#size' => 42, '#maxlength' => 255, '#default_value' => $edit['path'], '#field_prefix' => url(NULL, NULL, NULL, TRUE) . (variable_get('clean_url', 0) ? '' : '?q='), ); $form['redirect'] = array( '#type' => 'item', '#prefix' => '", // little bit of extra space ); $form['type'] = array( '#type' => 'fieldset', '#title' => t('Redirect Type'), '#collapsible' => TRUE, '#collapsed' => ($edit['type'] == PATH_REDIRECT_DEFAULT_TYPE), ); foreach (path_redirect_status_codes() as $key => $info) { $form['type'][]['type'] = array( '#type' => 'radio', '#title' => $info['title'], '#description' => $info['description'], '#return_value' => $key, '#default_value' => $edit['type'], ); } $form['type']['link'] = array( '#type' => 'markup', '#value' => t('
Find more information about http redirect codes here.
'), ); $form['rid'] = array( '#type' => 'hidden', '#value' => $edit['rid'], ); $form['submit'] = array( '#type' => 'submit', '#value' => $edit['rid'] ? t('Update redirect') : t('Create new redirect'), ); return $form; } function path_redirect_edit_validate($form_id, &$form_values, $form) { // Allow spaces in "from" path; encode everything but the query string $form_values['path'] = urlencode(preg_replace('/\?.*/', '', $form_values['path'])) . (strstr($form_values['path'], '?') ? preg_replace('/.*(\?)/', '$1', $form_values['path']) : ''); form_set_value($form['path'], $form_values['path']); if (trim($form_values['path']) == '') { form_set_error('path', t('You must enter a from path.')); } else { $path_error = ''; // The "from" path should not conflict with another redirect $result = path_redirect_load(NULL, $form_values['path']); if ($result && (!$form_values['rid'] || ($form_values['rid'] !== $result['rid']))) { $path_error .= ' '. t('The from path you entered is already redirected. You can edit this redirect instead.', array('@edit-page' => url('admin/build/path-redirect/edit/'. $result['rid']))); } // Check that the "from" path is valid and contains no # fragment if (strpos($form_values['path'], '#') !== FALSE) { $path_error .= ' '. t('You cannot redirect from a fragment anchor.'); } // Make sure "from" has the form of a local Drupal path if (!valid_url($form_values['path'])) { $path_error .= ' '. t('The redirect from path does not appear valid. This must be a local Drupal path.'); } if (!empty($path_error)) { form_set_error('path', $path_error); } } if (!valid_url($form_values['redirect']) && !valid_url($form_values['redirect'], TRUE) && $form_values['redirect'] != '?redirect=no
to the URL.'),
);
return system_settings_form($form);
}
function path_redirect_form_alter($form_id, &$form) {
if (variable_get('path_redirect_nodeapi_enabled', 0) && isset($form['#id']) && $form['#id'] == 'node-form' && user_access('administer redirects')) {
$form['redirects'] = array(
'#type' => 'fieldset',
'#access' => user_access('administer redirects'),
'#title' => t('URL redirects'),
'#collapsible' => TRUE,
'#collapsed' => !db_num_rows(path_redirect_node_redirects($form['#node']->nid)),
'#prefix' => '