R – How to create pathauto same stackoverflow.com

drupalpathauto

I know that stackoverflow.com uses the module pathauto. I would like to use pathauto in drupal to create pathauto uris. But I don't know how to do this.

Example: domain.com/node/1 after using pathauto then domain.com/article/1/title-node

Now I want to go to domain.com/article/1/??? then still it shows node 1 instead of show page not found.

Best Answer

EDIT - I got a better idea

I'm leaving my original answer below, but I think I got a better idea... you could implement hook_init() in a custom module of yours or custom_url_rewrite_inbound() in settings.php in order to strip your page request of the hyphened part, so that the url /article/1/my-title gets changed to /article/1 all the times. The alias from node/XXX to article/XXX would still be done by pathauto.

My original answer was:

I don't know if there is a module that already does that, but achieve what you want is quite easy. You should implement your own version of hook_menu() defining the function that will be triggered by an URL starting with "article" (for example "article/1/title-node").

For hook_menu, each bit separated by a slash is an argument that can be passed to the callback, so you will want to pass to the callback the number (so that the callback will load the proper node) and discard everything else.

Assuming you are using Drupal 6 Your menu item definition should look something like:

$item['article'] = array(
  'title' => 'My URL article redirect',
  'page callback' => 'name_of_my_callback_function',
  'page arguments' => array(1), //this passess the second bit of the URL
  'type' => MENU_CALLBACK,
);

Hope this helps!

Related Topic