如何创建相同的 pathauto stackoverflow.com

发布于 2024-08-11 21:02:55 字数 282 浏览 4 评论 0原文

我知道 stackoverflow.com 使用模块 pathauto。我想在 drupal 中使用 pathauto 来创建 pathauto uri。但我不知道该怎么做。

示例:使用pathauto后domain.com/node/1然后domain.com/article/1/title-node

现在我想转到domain.com /article/1/??? 然后它仍然显示节点 1 而不是显示未找到页面。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷荒 2024-08-18 21:02:56

编辑 - 我有一个更好的主意

我将原来的答案留在下面,但我认为我有一个更好的主意......你可以实现hook_init() 在您的自定义模块或 custom_url_rewrite_inbound() 在settings.php中,以去除页面请求的连字符部分,以便url /article/1/my-title 总是更改为 /article/1 。从node/XXX到article/XXX的别名仍然由pathauto完成。

我原来的答案是:

我不知道是否有一个模块已经做到了这一点,但实现你想要的东西很容易。您应该实现您自己的 hook_menu() 定义由以“article”开头的 URL(例如“article/1/title-node”)触发的函数。

对于 hook_menu,用斜杠分隔的每一位都是可以传递给回调的参数,因此您需要将数字传递给回调(以便回调将加载正确的节点)并丢弃其他所有内容。

假设您正在使用 Drupal 6 您的菜单项定义应类似于:

$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,
);

希望这有帮助!

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!

冰葑 2024-08-18 21:02:56

你不能使用 pathauto 来做到这一点,因为它所做的只是为你的 URL 创建别名。因此,您必须使用类似 http://example.com/article/1/title 与生成时完全相同。

Stack Overflow 的方式略有不同,他们只是忽略 URL 中作为问题标题出现的任何内容。他们可以做到这一点,因为 URL 解析器完全不同。对于 Drupal,您可能想要寻找另一个模块或推出您自己的模块。 Pathauto 只需利用普通的 URL 别名并为您自动生成它们。不多也不少。

在 drupal.org 上搜索处理 URL 的模块会产生相当多的匹配项。其中一些可能会做你想做的事。

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.

瞄了个咪的 2024-08-18 21:02:56

我假设你想要做的是有一个像 http://example.com/article/ID/title 这样的 url 结构,其中 title 部分是几乎被忽略,无论为title输入什么,它都会转到http://example.com/article/ID
可以做的是设置一个视图,其路径为http://example.com/article它接受 ID 作为参数,用于指定单个文章 - 最有可能是 nid。然后它应该忽略后面的任何内容。

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.

叫思念不要吵 2024-08-18 21:02:56

您是否尝试过全局重定向,这确保您只能看到别名路径?
http://drupal.org/project/globalredirect
子路径别名又如何呢?它也允许您使用别名作为子路径。
例如,假设 node/1 别名是 blog/johndoe/my-first-post ,您可以使用 blog/johndoe/my-first-post/edit 编辑它
http://drupal.org/project/subpath_alias

我想您可以使用这些模块。

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_alias

i guess you can work with these modules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文