'. 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.") .'
'; } } /** * Implementation of hook_init * * Early checking of URL requested. * If a match is found, user is redirected using drupal_goto() */ function path_redirect_init() { // Extract the Drupal path and query string from the request URI $path = substr(request_uri(), strlen($GLOBALS['base_path'])); if (preg_match('/^\?q=/', $path)) { $path = preg_replace(array('/^\?q=/', '/&/'), array('', '?'), $path, 1); } $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 there is no match against path and query string, check just the path if (!$r) { $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)))); } // only redirect if allow_bypass is off or bypass is not requested if ($r && !(variable_get('path_redirect_allow_bypass', 0) && !empty($_GET['redirect']) && $_GET['redirect'] == 'no') && url($r->redirect) != url($path)) { if (variable_get('path_redirect_redirect_warning', 0)) { drupal_set_message(t('This page has been moved. You may want to update your bookmarks.')); } if (function_exists('drupal_goto')) { // if there's a result found, do the redirect unset($_REQUEST['destination']); drupal_goto($r->redirect, ($r->query ? $r->query: NULL), ($r->fragment ? $r->fragment : NULL), $r->type); } else { // page caching is turned on so drupal_goto() (common.inc) hasn't been loaded path_redirect_goto($r->redirect, ($r->query ? $r->query: NULL), ($r->fragment ? $r->fragment : NULL), $r->type); } } else if ($r && url($r->redirect) == url($path)) { 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));
}
else if ($r && variable_get('path_redirect_allow_bypass', 0) && !empty($_GET['redirect']) && $_GET['redirect'] === 'no') {
drupal_set_message(t('This page is redirected to:') .' '. l($r->redirect, $r->redirect, NULL, ($r->query ? $r->query: NULL), ($r->fragment ? $r->fragment : NULL)) .'');
}
}
/**
* 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/settings/path-redirect',
'title' => t('URL redirects'),
'description' => t('Configure behavior for URL redirects'),
'callback' => 'drupal_get_form',
'callback arguments' => 'path_redirect_settings',
'access' => $access,
);
}
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($rid = FALSE) {
$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' => '3')
);
$result = pager_query('SELECT rid, path, redirect, query, fragment, type FROM {path_redirect}'. tablesort_sql($header), 50);
$count = db_num_rows($result);
$types = path_redirect_status_codes();
while ($r = db_fetch_object($result)) {
$path = $r->path;
$redirect = $r->redirect;
$query = $r->query ? "?$r->query" : '';
$fragment = $r->fragment ? "#$r->fragment" : '';
$rows[] = array(
htmlspecialchars(urldecode($path)),
htmlspecialchars($redirect . $query . $fragment),
$types[$r->type]['title'],
array('data' => l(t('test'), preg_replace('/\?.*/', '', urldecode($path)), NULL, strstr($path, '?') ? preg_replace('/.*\?/', '', $path) : NULL)),
array('data' => l(t('edit'), 'admin/build/path-redirect/edit/'. $r->rid)),
array('data' => l(t('delete'), 'admin/build/path-redirect/delete/'. $r->rid)),
);
}
if (!$count) {
$rows[] = array(array('data' => t('No redirects have been defined.'), 'colspan' => '6'));
}
$output = theme('table', $header, $rows, array('class' => 'path-redirects'));
$output .= ''. l(t('Add new 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 (strstr($form_values['path'], '#')) { $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' => '