Drupal provides users complete control over URLs through aliasing. This feature is typically used to make URLs human-readable or easy to remember. For example, one could map the relative URL 'node/1' onto 'about'. Each system path can have multiple aliases.

"); case 'admin/path/add': return t('

Enter the path you wish to create the alias for, followed by the name of the new alias.

'); case 'admin/help#path': return t("

Background

A very powerful feature of Drupal is the ability to have control over all paths. The path module is the tool that provides this functionality and is part of the basic Drupal installation, although it is not enabled by default. Some examples of re-mapping paths are:

user/login => login

image/tid/16 => store

taxonomy/term/7+19+20+21 => store/products/whirlygigs

node/3 => contact

This functionality integrates seamlessly into node forms and also provides the administrator an interface to view all aliases that have been created.

Aliases have a many to one relationship with their original Drupal URLs. In other words you can have many different aliases map to a single path. An example of where a multiple aliases come in handy is creating a standard RSS feed URL:

node/feed => rss.xml
node/feed => index.rdf

When Drupal generates links for a path with multiple aliases it will choose the first alias created per system URL. So in our above example, Drupal would use rss.xml as the default alias rather than index.rdf. To change this behavior, delete the aliases for node/feed and create the index.rdf alias before rss.xml.

Permissions

Two permissions are related to URL aliasing: create url aliases and administer url aliases.

  1. create url aliases - Allows users to create aliases for nodes. Enabling this permission will display a path field to the user in any node form, allowing them to enter an alias for that node. They will be able to edit/delete the alias after it is created using the same form.
  2. administer url aliases - Allows users to access the alias administration interface. This interface displays all aliases and provides a way to create and modify them. This is also the location to build aliases for things other than nodes. For example, you can create an alias for a taxonomy URL or even re-map the admin path (although the original admin path will still be accessible since aliases do not cancel out original paths).

Mass URL aliasing

Drupal also comes with user defined mass URL aliasing capabilities. You might like to see completely different URLs used by Drupal, or even URLs translated to the visitors' native language, in which case this feature is handy. Only an administrator with access to the website source code can set up this kind of aliases. You can define a conf_url_rewrite function in your configuration file (eg. sites/default/settings.php), following this example:

function conf_url_rewrite(\$path, \$mode = 'incoming') {
  if (\$mode == 'incoming') { // URL coming from a client
    return preg_replace('!^display/(\\d+)\$!', 'node/\\1', \$path);
  }
  else { // URL going out to a client
    \$aliased = preg_replace('!^node/(\\d+)\$!', 'display/\\1', \$path);
    if (\$aliased != \$path) { return \$aliased; }
  }
}

This function will shorten every node/\$node_id type of URL to display/\$node_id. Individual URL aliases defined on the browser interface of Drupal take precedence, so if you have the 'contact' page alias from the example above, then the display/3 alias will not be effective when outgoing links are created. Incoming URLs however always work with the mass URL aliased variant. Only the 'incoming' and 'outgoing' modes are supposed to be supported by your conf_url_rewrite function.

You cannot only use this feature to shorten the URLs, or to translate them to you own language, but also to add completely new subURLs to an already existing module's URL space, or to compose a bunch of existing stuff together to a common URL space. You can create a news section for example aliasing nodes and taxonomy overview pages falling under a 'news' vocabulary, thus having news/15 and news/sections/3 instead of node/15 and taxonomy/term/3. You need extensive knowledge of Drupal's inner workings and regular expressions though to make such advanced aliases.

"); } } /** * Implementation of hook_menu(). */ function path_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array('path' => 'admin/path', 'title' => t('url aliases'), 'callback' => 'path_admin', 'access' => user_access('administer url aliases')); $items[] = array('path' => 'admin/path/edit', 'title' => t('edit alias'), 'callback' => 'path_admin_edit', 'access' => user_access('administer url aliases'), 'type' => MENU_CALLBACK); $items[] = array('path' => 'admin/path/delete', 'title' => t('delete alias'), 'callback' => 'path_admin_delete', 'access' => user_access('administer url aliases'), 'type' => MENU_CALLBACK); $items[] = array('path' => 'admin/path/list', 'title' => t('list'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'admin/path/add', 'title' => t('add alias'), 'callback' => 'path_admin_edit', 'access' => user_access('administer url aliases'), 'type' => MENU_LOCAL_TASK); } return $items; } /** * Menu callback; presents an overview of all URL aliases. */ function path_admin() { print theme('page', path_overview()); } /** * Menu callback; handles pages for creating and editing URL aliases. */ function path_admin_edit($pid = 0) { if ($_POST['op'] == t('Create new alias') || $_POST['op'] == t('Update alias')) { $output = path_save($_POST['edit']); } elseif ($pid) { $alias = path_load($pid); drupal_set_title($alias['dst']); $output = path_form(path_load($pid)); } else { $output = path_form(); } print theme('page', $output); } /** * Menu callback; handles deletion of an URL alias. */ function path_admin_delete($pid = 0) { db_query('DELETE FROM {url_alias} WHERE pid = %d', $pid); drupal_set_message(t('The alias has been deleted.')); drupal_goto('admin/path'); } /** * Set an aliased path for a given Drupal path, preventing duplicates. */ function path_set_alias($path = NULL, $alias = NULL, $pid = NULL) { if ($path && !$alias) { db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path); drupal_rebuild_path_map(); } else if (!$path && $alias) { db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias); drupal_rebuild_path_map(); } else if ($path && $alias) { $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s'", $path)); $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias)); // We have an insert: if ($path_count == 0 && $alias_count == 0) { db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias); drupal_rebuild_path_map(); } else if ($path_count >= 1 && $alias_count == 0) { if ($pid) { db_query("UPDATE {url_alias} SET dst = '%s', src = '%s' WHERE pid = %d", $alias, $path, $pid); } else { db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias); } drupal_rebuild_path_map(); } else if ($path_count == 0 && $alias_count == 1) { db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias); drupal_rebuild_path_map(); } else if ($path_count == 1 && $alias_count == 1) { // This will delete the path that alias was originally pointing to: path_set_alias(NULL, $alias); path_set_alias($path); path_set_alias($path, $alias); } } } /** * Return a form for editing or creating an individual URL alias. */ function path_form($edit = '') { $form .= form_textfield(t('Existing system path'), 'src', $edit['src'], 50, 64, t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1+2.')); $form .= form_textfield(t('New path alias'), 'dst', $edit['dst'], 50, 64, t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.')); if ($edit['pid']) { $form .= form_hidden('pid', $edit['pid']); $form .= form_submit(t('Update alias')); } else { $form .= form_submit(t('Create new alias')); } return form($form); } /** * Implementation of hook_nodeapi(). * * Allows URL aliases for nodes to be specified at node edit time rather * than through the administrative interface. */ function path_nodeapi(&$node, $op, $arg) { if (user_access('create url aliases') || user_access('administer url aliases')) { switch ($op) { case 'validate': $node->path = trim($node->path); if ($node->path && !valid_url($node->path)) { form_set_error('path', t('The path is invalid.')); } else if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s' AND src != '%s'", $node->path, "node/$node->nid"))) { form_set_error('path', t('The path is already in use.')); } break; case 'form pre': $output = form_textfield(t('Path alias'), 'path', $node->path, 60, 250, t('Optionally specify an alternative URL by which this node can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.')); if ($node->path) { $output .= form_hidden('pid', db_result(db_query("SELECT pid FROM {url_alias} WHERE dst = '%s'", $node->path))); } return $output; case 'load': $path = "node/$node->nid"; $alias = drupal_get_path_alias($path); if ($alias != $path) { $node->path = $alias; } break; case 'insert': // Don't try to insert if path is NULL. We may have already set // the alias ahead of time. if ($node->path) { path_set_alias("node/$node->nid", $node->path); } break; case 'update': path_set_alias("node/$node->nid", $node->path, $node->pid); break; case 'delete': $path = "node/$node->nid"; if (drupal_get_path_alias($path) != $path) { path_set_alias($path); } break; } } } /** * Implementation of hook_perm(). */ function path_perm() { return array('create url aliases', 'administer url aliases'); } /** * Return a listing of all defined URL aliases. */ function path_overview() { $sql = 'SELECT * FROM {url_alias}'; $header = array( array('data' => t('Alias'), 'field' => 'dst', 'sort' => 'asc'), array('data' => t('System'), 'field' => 'src'), array('data' => t('Operations'), 'colspan' => '2') ); $sql .= tablesort_sql($header); $result = pager_query($sql, 50); $destination = drupal_get_destination(); while ($data = db_fetch_object($result)) { $rows[] = array($data->dst, $data->src, l(t('edit'), "admin/path/edit/$data->pid", array(), $destination), l(t('delete'), "admin/path/delete/$data->pid", array(), $destination)); } if ($pager = theme('pager', NULL, 50, 0, tablesort_pager())) { $rows[] = array(array('data' => $pager, 'colspan' => '4')); } if (!$rows) { $rows[] = array(array('data' => t('No URL aliases available.'), 'colspan' => '4')); } return theme('table', $header, $rows); } /** * Fetch a specific URL alias from the database. */ function path_load($pid) { return db_fetch_array(db_query('SELECT * FROM {url_alias} WHERE pid = %d', $pid)); } /** * Verify that a new URL alias is valid, and save it to the database. */ function path_save($edit) { $src = $edit['src']; $dst = $edit['dst']; $pid = $edit['pid']; if (!valid_url($src)) { form_set_error('src', t('The system path %path is invalid.', array('%path' => theme('placeholder', $src)))); } if (!valid_url($dst)) { form_set_error('dst', t('The alias %alias is invalid.', array('%alias' => theme('placeholder', $dst)))); } if (db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE pid != %d AND dst = '%s'", $pid, $dst))) { form_set_error('dst', t('The alias %alias is already in use.', array('%alias' => theme('placeholder', $dst)))); } if (form_get_errors()) { return path_form($edit); } else { path_set_alias($src, $dst, $pid); drupal_set_message(t('The alias has been saved.')); drupal_goto('admin/path'); } } ?>