SpinSpire logo

URL rewriting within Drupal

You probably already know how to do URL rewriting using Apache. Here's how you can rewrite paths within Drupal. Added a function named "custom_url_rewrite_inbound" to your settings.php file.

function custom_url_rewrite_inbound(&$result, $path, $path_language) {
  // convert u/xyz to user/xyz/edit
  // $result = preg_replace('/^u\/(.*)/', 'user/${1}/edit', $path);
  // convert u/xyz to user/xyz/view
  $result = preg_replace('/^u\/(.*)/', 'user/${1}/view', $path);
}

It has to be in settings.php file so that this function is defined even before Drupal core executes. So now if you hit URL http://localhost/u/555, it will take you to http://localhost/user/555/view

D7 UPDATE: In D7, you don't have to touch settings.php. You can simply implement the hook_url_inbound_alter.