I'm looking for a way to build what I call 'StackOverflow style' url's. In my definition, that means:
1) A node has a path alias containing both a unique identifier and the node title. For instance, if I'm writing a story and every chapter is a node, chapter 4 (titled "The day I became a lobster") would get www.example.com/chapter/4/the-day-i-became-a-lobster.
2) If a user knows the chapter, but not the title, he can just go to www.example.com/chapter/4, which will redirect to www.example.com/chapter/4/the-day-i-became-a-lobster.
3) If the title changes (www.example.com/chapter/4/the-day-i-became-a-lobster becomes www.example.com/chapter/4/the-day-i-became-a-penguin), or if the url is mistyped (www.example.com/chapter/4/the-day-i-become-a-lobster), the old or incorrect url redirects to the new / correct url.

I want this behavior because:
- In some cases, my users know a product number but not the title. This way they can easily figure out the url.
- Having the title in the url is good for SEO and user experience.

Is is possible to do this with Redirect, or with a custom module based on the Redirect API? If not, would you consider this a feature request, or is it out of scope?

Comments

bryancasler’s picture

subscribe

Mamouri’s picture

This is very necessary for my website too which might has lot's of url change. I use pathauto with a format like this:

/[TITLE]/b/[NID]/

So I can easily redirect all 404 page to /node/[NID]

donquixote’s picture

cool stuff, i am interested.

donquixote’s picture

What if the alias is
node/[nid]/[title]
Then if you type the wrong title, it will still find the correct node by the first two fragments: If node/123/i-am-hamster is not a valid path, then Drupal falls back to node/123.
The redirect module will now detect that node/123 has the alias node/123/i-am-rabbit, and do the redirect.

All of this only works if the alias always has the system path as a prefix.
If you want your chapter/4 stuff to work, you need to make chapter/%chapter_id a valid router path via hook_menu(). And then a mechanic to store aliases for whatever entity type you have at this path. (i am still so totally D6, I have no idea what would be a usual pattern for entity paths and aliases)

marcvangend’s picture

Meanwhile I asked the same question over at Drupal Answers, where "Greg" came up with the idea of hooking into the 404 behavior. It still requires custom code (it would be great if it's manageable from the UI) but it works: no need for extra hard-coded menu items and the method is very flexible.

donquixote’s picture

What about this was a feature in pathauto.
You would specify a pattern like this:
chapter/[nid]+/[title]
Where everything after the + symbol would be the "informal" part of the alias.

marcvangend’s picture

Re#6: I was thinking the same: While this is essentially redirect-behavior (that's why I posted this in the Redirect issue queue), for end users it makes sense to integrate these settings with pathauto.

That said, I'm not sure what the best UI solution would be. I understand your suggestion with the + sign, but there is a command line / regex feel to it that might scare less technical users. If we're going to do this, let's invite some UI/UX experts to the party.

Dave Reid’s picture

So...what I'd recommend:

Setting a pathauto alias to chapter/[node:nid]/[node:title] and then create a custom menu callback at chapter/%node that redirects to url('node/' . $node->nid). Also ensure that Redirect has its 'Create redirects when URL aliases change' setting is enabled.

Possibly you could add an .htaccess rewrite rule for '^chapter/(.*)$' that gets rewritten to 'node/$1' and then once the global redirections are re-enabled in Redirect.module it would be handled for you.

marcvangend’s picture

Dave, thanks for your input.

I think the menu callback approach is perfect or developers, but less for non-technical users. Also, a truly generic solution would also allow for a dynamically defined arg(0), for instance if your path is [node:type]/[node:nid]/[node:title]. I'm not sure if you could do that with a menu callback (can you define $item['%'] in hook_menu?).

Looks like I'll have to to some tests soon.

donquixote’s picture

I think that root namespace is too polluted. For instance, if you want a node type called "admin", you collide with the system path of the same name.
For the same reason, we have user/1 but users/admin, I think. So that your username can be "1" and you still get a valid alias..
(yeah some of this is far-fetched, I just did not spend enough time looking for examples)

marcvangend’s picture

You're right, you have point there.

Pere Orga’s picture

Category: Support request » Feature request
Issue summary: View changes
Status: Active » Closed (works as designed)

Is this still wanted? If it's not already possible with Pathauto and tokens this could be more a feature request for the Token module. I see this much more related to path aliases than to redirects.

Currently with aliases, an url like /alias-name/<random characters> is showing me the content in /alias-name.