views:

229

answers:

5

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.

+2  A: 

You can't do that with pathauto since all it does is creating aliases for your URLs. So you have to use an URL like http://example.com/article/1/title exactly as it was generated.

Stack Overflow goes a slightly different way in the sense that they simply ignore whatever comes as question title in the URL. They can do that since the URL parser is entirely different. For Drupal you may want to look for another module or roll your own. Pathauto just taps into the normal URL aliases and auto-generates them for you. No more, no less.

A search for modules which deal with URLs on drupal.org yields quite a few matches. Some of them might do what you want.

Joey
+4  A: 

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!

mac
Closed paren after 'name_of_my_callback_function' isn't needed, i suppose.
Kuroki Kaze
Fixed, thank you!
mac
+1 - both are good solutions. In case this is needed for a heavy traffic site, the newer suggestion (stripping last part before processing) could also be done by means of `custom_url_rewrite_inbound` (http://api.drupal.org/api/function/custom_url_rewrite_inbound/6) instead of `hook_init()`. That would save some processing cycles, as the adjustment would happen before any other path processing, but at the price of having to put that somewhat weird 'pseudo hook' into settings.php.
Henrik Opel
If the paths are reliable enough (e.g. no non article page starting with 'article/[number]'), it could be even more effective to do the rewriting completely outside of Drupal/PHP by means of `mod_rewrite`. Probably most effective, but harder to maintain in case of changes.
Henrik Opel
@Hendrik: added your suggestion on custom\_url\_rewrite\_inbound() to the answer (and upvoted your comment! ;)
mac
+1  A: 

I am going on the assumption that what you want to do is have an url structure like http://example.com/article/ID/title where the title part is pretty much ignored and it will go to http://example.com/article/ID no matter what is entered for title. What you could do is set up a view that has the path http://example.com/article which accepts ID as an argument which is used to specify an individual article - most likely by the nid. It should then ignore anything that comes after it.

Andrew
That is a solution too. It makes sense if you use views for something else than this too... otherwise is like going bird-hunting like a bazooka. [views is a heavyweight module and it's not worth installing it if all you have to do is this simple trick] :)
mac
A: 

By the way, StackOverflow doesn't use Drupal - it's based on ASP.NET MVC framework.

MaxVT
A: 

have you tried global redirect, which ensures you only see the aliased path? http://drupal.org/project/globalredirect and what about subpath alias? it allows you to use alias as subpaths also. for example, say node/1 alias is blog/johndoe/my-first-post , you could edit it using blog/johndoe/my-first-post/edit http://drupal.org/project/subpath%5Falias

i guess you can work with these modules.

barraponto

related questions