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() { // check through the redirects // see if this page is one of those // Assumption: // The order of the variables matches that given // OK, clean up the request so that it's consistent - there might be a cleaner way of doing this using regular expressions $presentedPath = urldecode(trim($_SERVER['REQUEST_URI'], '/')); if (substr($presentedPath, 0, 3) == "?q=") { $presentedPath = substr($presentedPath, 3); $ampPos = strpos($presentedPath, "&"); if ($ampPos !== FALSE) { $presentedPath = substr($presentedPath, 0, $ampPos) . "?" . substr($presentedPath, $ampPos + 1); } } $testURL = drupal_get_normal_path($presentedPath); // Use the base case $r = db_fetch_object(db_query('SELECT redirect, query, fragment, type FROM {path_redirect} WHERE path = "%s"', $testURL)); if ($r) { drupal_goto($r->redirect, ($r->query ? $r->query: NULL), ($r->fragment ? $r->fragment : NULL), $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'), 'access' => $access, 'callback' => 'path_redirect_admin', 'description' => t('Redirect users from one URL to another.'), ); $items[] = array( 'path' => 'admin/build/path_redirect/list', 'title' => t('List'), 'access' => $access, 'weight' => -3, 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items[] = array( 'path' => 'admin/build/path_redirect/new', 'title' => t('Add redirect'), 'access' => $access, 'weight' => 2, 'type' => MENU_LOCAL_TASK, ); } else { if (arg(0) == 'admin' && arg(1) == 'build' && arg(2) == 'path_redirect' && arg(3) == 'edit') { $items[] = array( 'path' => 'admin/build/path_redirect/edit', 'title' => t('Edit'), 'access' => $access, 'callback' => 'path_redirect_admin', 'type' => MENU_LOCAL_TASK, 'weight' => 9, ); } } return $items; } /** * Implementation of hook_perm */ function path_redirect_perm() { return array('administer redirects'); } /** * Callback for administration pages * * @param $rid * redirect id * @param $op * operation: delete * @return * themed output for page */ function path_redirect_admin($rid = FALSE, $op = FALSE) { if ($rid) { $breadcrumbs = array(l(t('Home'), '/'), l(t('Administer'), 'admin'), l(t('URL redirects'), 'admin/build/path_redirect')); if ($rid == 'new') { drupal_set_breadcrumb($breadcrumbs); return drupal_get_form('path_redirect_edit'); } else { $rid = db_escape_string($rid); $redirect = db_fetch_array(db_query('SELECT * FROM {path_redirect} WHERE rid = %d', $rid)); if ($redirect && $op != 'delete') { drupal_set_breadcrumb($breadcrumbs); return drupal_get_form('path_redirect_edit', $redirect); } elseif ($redirect && $op == 'delete') { return drupal_get_form('path_redirect_delete', $redirect); } else { drupal_not_found(); } } } $result = pager_query('SELECT rid, path, redirect, query, fragment, type FROM {path_redirect} ORDER BY path', 50); $count = db_num_rows($result); $types = path_redirect_error_list(); while ($r = db_fetch_object($result)) { $path = drupal_get_path_alias($r->path); $redirect = drupal_get_path_alias($r->redirect); $query = $r->query ? "?$r->query" : ''; $fragment = $r->fragment ? "#$r->fragment" : ''; $rows[] = array( $path, $redirect.$query.$fragment, $types[$r->type]['title'], array('data' => l(t('test'), $r->path, array(), $r->query ? $r->query : NULL, $r->fragment ? $r->fragement : NULL)), array('data' => l(t('edit'), 'admin/build/path_redirect/edit/'. $r->rid)), array('data' => l(t('delete'), 'admin/build/path_redirect/edit/'. $r->rid .'/delete')), ); } $header = array(t('From'), t('To'), t('Type'), array('data' => t('Operations'), 'colspan' => '3')); if ($count) { $output .= ''. theme('table', $header, $rows) .'
'; } else { $output .= ''. t('No entries found.') .'
'; } $output .= ''. l(t('Add New Redirect'), 'admin/build/path_redirect/new') .'
'; $output .= theme('pager'); return $output; } function path_redirect_edit($edit = array()) { $default_type = 301; if (!empty($edit)) { $form['rid'] = array( '#type' => 'hidden', '#value' => $edit['rid'], ); } else { // if it's a new entry // set up the default $edit['type'] = $default_type; } $form['path'] = array( '#type' => 'textfield', '#title' => t('From'), '#description' => t('Enter a Drupal path or path alias to redirect'), '#default_value' => drupal_get_path_alias($edit['path']), ); $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'] == $default_type), ); foreach (path_redirect_error_list() 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['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); return $form; } function path_redirect_edit_validate($form_id, &$form_values) { if (trim($form_values['path']) == '') { form_set_error('path', t('You must enter a from path.')); } //check that the from url is valid and contains no # or ? elseif (strstr($form_values['path'], '#')) { form_set_error('path', t('You cannot redirect from a fragment anchor.')); } // Remove no question mark requirement // elseif (strstr($form_values['path'], '?')) { // form_set_error('path', t('You cannot currently include a query in your redirect from path.')); // } elseif (!valid_url($form_values['path'])) { form_set_error('path', t('The redirect from path does not appear valid. This must be a local Drupal path.')); } if (!valid_url($form_values['redirect']) && !valid_url($form_values['redirect'], TRUE)) { form_set_error('redirect', t('The redirect to path does not appear valid.')); } $form_values['path'] = drupal_get_normal_path($form_values['path']); $form_values['redirect'] = drupal_get_normal_path($form_values['redirect']); //check that there there are no redirect loops if ($form_values['path'] == $form_values['redirect']) { form_set_error('redirect', t('You are attempting to redirect the page to itself. This will result in an infinite loop -- so don\'t do it!')); } // per issue #119249 $form_values['path'] = str_replace("+", " ", $form_values['path']); } function path_redirect_edit_submit($form_id, &$form_values) { path_redirect_save($form_values); drupal_set_message(t('Redirect has been saved.')); drupal_goto('admin/build/path_redirect'); } function path_redirect_save($edit) { if (!$edit['rid']) { $edit['rid'] = db_next_id('{path_redirect}'); } path_redirect_delete_do($edit['rid']); return db_query('INSERT INTO {path_redirect} (rid, path, redirect, query, fragment, type) VALUES (%d, "%s", "%s", "%s", "%s", "%s")', $edit['rid'], $edit['path'], $edit['redirect'], $edit['query'], $edit['fragment'], $edit['type']); } function path_redirect_delete_do($rid) { return db_query('DELETE FROM {path_redirect} WHERE rid = %d', $rid); } function path_redirect_delete($redirect) { $form['rid'] = array( '#type' => 'value', '#value' => check_plain($redirect['rid']), ); return confirm_form($form, t('Are you sure you want to delete this redirect?'), 'admin/build/path_redirect', NULL, t('Delete it!'), t('Cancel')); } function path_redirect_delete_submit($form_id, $form_values) { path_redirect_delete_do($form_values['rid']); drupal_set_message(t('Redirect item has been deleted.')); drupal_goto('admin/build/path_redirect'); } /** * Return an array of 300-range error codes * placed here for clarity */ function path_redirect_error_list() { $errors = array( 300 => array('title' => t('300 Multiple Choices'), 'description' => t('The request is ambiguous and needs clarification as to which resource was requested.')), 301 => array('title' => t('301 Moved Permanently'), 'description' => t('Moved Permanently. The resource has permanently moved elsewhere, the response indicates where it has gone to. Recommended.')), 302 => array('title' => t('302 Found'), 'description' => t('The resource has temporarily moved elsewhere, the response indicates where it is at present. This is Drupal\'s default redirect type.')), 303 => array('title' => t('303 See Other'), 'description' => t('See Other/Redirect. A preferred alternative source should be used at present.')), 304 => array('title' => t('304 Not Modified'), 'description' => t('The server has identified from the request information that the client\'s copy of the information is up-to-date and the requested information does not need to be sent again.')), 305 => array('title' => t('305 Use Proxy'), 'description' => t('The request must be sent through the indicated proxy server.')), 306 => array('title' => t('307 Temporary Redirect'), 'description' => t('The resource has temporarily moved elsewhere, the response indicates where it is at present. Client should still use this URL.')), ); return $errors; }