URL rewriting within Drupal

URL rewriting within Drupal

Submitted by Jitesh Doshi on Fri, 10/07/2011 - 22:33

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.

 

Jitesh Doshi

Profile picture for user Jitesh Doshi
Managing Partner & CTO
  • A seasoned technology entrepreneur and enthusiast
  • A regular speaker at industry conferences and universities
  • Host and organizer of technology user groups
  • Active in management of non-profit organizations serving the local community
  • Leader and contributor for multiple open-source projects
  • Expert in cloud, application integration, web and mobile technologies
  • Author of open-source projects, including on Drupal.org - Popular Tags and PRLP.
  • Developed several highly successful software platforms and frameworks for clients