custom_url_rewrite

developer/hooks/core.php, line 1576

Versions
5
custom_url_rewrite($op, $result, $path)

custom_url_rewrite is not a hook. It is a function you can add to settings.php to manage aliases with some code.

Parameters

$op Can be 'alias' or 'source'. For 'alias', an alias need to be returned. For 'source', return the Drupal path based on the alias passed in. 'source' is first called before modules are loaded and the menu system is initialized and $_GET['q'] subsequently will be the value of what's returned from this function call.

$result For op 'alias', this is the alias of the path from the database. For op 'source', this is the Drupal path based on the database. If there is no match in the database it'll be the same as $path for both ops.

$path The path to be sourced/aliased.

Return value

The changed path. Even if it's not changed, it must be returned.

Related topics

▾ 2 functions call custom_url_rewrite()

drupal_get_normal_path in includes/path.inc
Given a path alias, return the internal path it represents.
drupal_get_path_alias in includes/path.inc
Given an internal Drupal path, return the alias set by the administrator.

Code

<?php
function custom_url_rewrite($op, $result, $path) {
  global $user;
  if ($op == 'alias') {
    // Overwrite a menu path already defined, with this code, if the user
    // goes to 'tracker', the page 'views/tracker' will be displayed instead
    // without any redirection. To achieve this, only the op source act is a
    // must, this is optional.
    if ($path == 'views/tracker') {
      return 'tracker';
    }
    // Change all 'node' to 'article'.
    if (preg_match('|^node/(.*)|', $path, $matches)) {
      return 'article'. $matches[1];
    }
    // Create a path called 'e' which lands the user on her edit page.
    if ($path == 'user/'. $user->uid .'/edit') {
      return 'e';
    }
  }
  if ($op == 'source') {
    if ($path == 'tracker') {
      // Change 'tracker' to 'views/tracker' when a request lands.
      return 'views/tracker';
    }
    // Change all 'node' to 'article'.
    if (preg_match('|^article(/.*)|', $path, $matches)) {
      return 'node'. $matches[1];
    }
    // Create a path called 'e' which lands the user on her edit page.
    if ($path == 'e') {
      return 'user/'. $user->uid .'/edit';
    }
  }
  // Do not forget to return $result!
  return $result;
}
?>
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.